Skip to content

Commit

Permalink
fix error in cleanup, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
smoes committed Aug 21, 2024
1 parent dbfe112 commit 79c9f4f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Efx follows the following principles:
To use `Efx` in your project, add this to your dependencies in `mix.ex`:

```elixir
{:efx, "~> 0.2.6"}
{:efx, "~> 0.2.7"}
```

If you want to have proper formatting of the `Efx.defeffect/2` macro, you can add
Expand Down
7 changes: 6 additions & 1 deletion lib/efx_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,16 @@ defmodule EfxCase do
Internal.init(pid)

on_exit(fn ->
Internal.verify_mocks!(pid)
res = Internal.verify_mocks(pid)

unless unquote(async?) do
MockState.clean_after_test()
end

case res do
:ok -> :ok
{:error, error} -> raise error
end
end)
end

Expand Down
7 changes: 7 additions & 0 deletions lib/efx_case/internal.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
defmodule EfxCase.Internal do
alias EfxCase.MockState

def verify_mocks(pid) do
verify_mocks!(pid)
:ok
rescue
e -> {:error, e}
end

def verify_mocks!(pid) do
MockState.verify_called!(pid)
end
Expand Down
16 changes: 13 additions & 3 deletions lib/efx_case/mock_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ defmodule EfxCase.MockState do
invocations as well as store mocks as global when a test is flagged as
async.
"""
alias ExUnit.Assertions
alias EfxCase.Mock
alias EfxCase.Mock.MockedFun

require ExUnit.Assertions

use Agent

@typedoc """
Expand Down Expand Up @@ -44,7 +48,9 @@ defmodule EfxCase.MockState do
mock

{:error, :function_not_in_mock} ->
raise "Not matching function found for #{behaviour}: #{fun_identifier}/#{arity}"
Assertions.flunk(
"Not matching function found for #{behaviour}: #{fun_identifier}/#{arity}"
)
end

new_pid_state = Map.put(pid_state, behaviour, new_mock)
Expand All @@ -70,7 +76,9 @@ defmodule EfxCase.MockState do
{ret, Map.put(state, scope, new_pid_state)}
else
{:error, _err} ->
raise "no mock found for #{inspect(behaviour)}/#{inspect(fun_identifier)} in scope #{inspect(pid)} with state #{inspect(state)}"
Assertions.flunk(
"no mock found for #{inspect(behaviour)}/#{inspect(fun_identifier)} in scope #{inspect(pid)} with state #{inspect(state)}"
)
end
end)

Expand Down Expand Up @@ -115,7 +123,9 @@ defmodule EfxCase.MockState do
unsatisfied = Mock.get_unsatisfied(mock)

unless Enum.empty?(unsatisfied) do
raise "expectations for #{inspect(behaviour)} were not meat: \n #{parse(unsatisfied)}"
Assertions.flunk(
"expectations for #{inspect(behaviour)} were not met: \n #{parse(unsatisfied)}"
)
end
end)
end
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Efx.MixProject do
use Mix.Project

@version "0.2.6"
@version "0.2.7"
@github_page "https://github.com/bravobike/efx"

def project do
Expand Down Expand Up @@ -56,7 +56,7 @@ defmodule Efx.MixProject do

defp package do
[
files: ~w(mix.exs README.md lib),
files: ~w(mix.exs README.md lib .formatter.exs),
licenses: ["Apache-2.0"],
links: %{
"GitHub" => @github_page
Expand Down
2 changes: 1 addition & 1 deletion test/efx_case/mock_state_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule EfxCase.MockStateTest do
end

test "raises if if unsatisfied" do
assert_raise(RuntimeError, fn ->
assert_raise(ExUnit.AssertionError, fn ->
add_fun(num_expected_calls: 1)
MockState.verify_called!(self())
end)
Expand Down

0 comments on commit 79c9f4f

Please sign in to comment.