Skip to content

Commit

Permalink
Fix issue with verify and monitoring (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin-kargo authored Sep 30, 2020
1 parent d89b6d2 commit 5211b68
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ assert Calculator.add(1, 3) == {:add, 1, 3}
assert Calculator.add(4, 5) == {:add, 4, 5}
```

With `use Mimic`, verification `expect/4` function call of is done automatically on test case end. `verify!/1` can be used in case custom verification timing required:

```elixir
Calculator
|> expect(:add, 2, fn x, y -> {:add, x, y} end)

# Will raise error because Calculator.add is not called
# ** (Mimic.VerificationError) error while verifying mocks for #PID<0.3182.0>:
# * expected Calculator.add/2 to be invoked 1 time(s) but it has been called 0 time(s)
verify!()
```


### Reject

One may want to reject calls to a specific function. `reject/1` can be used to achieved this behaviour.
Expand Down
9 changes: 8 additions & 1 deletion lib/mimic/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,14 @@ defmodule Mimic.Server do
end

def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do
{:noreply, clear_data_from_pid(pid, state)}
new_state =
if MapSet.member?(state.verify_on_exit, pid) do
state
else
clear_data_from_pid(pid, state)
end

{:noreply, new_state}
end

def handle_info(msg, state) do
Expand Down
24 changes: 24 additions & 0 deletions test/edge_case_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Mimic.EdgeCase do
use ExUnit.Case
import Mimic

describe "auto verification" do
test "should verify_on_exit! correctly even when stub is called before (simulation test)" do
set_mimic_private()
parent_pid = self()

spawn_link(fn ->
stub(Calculator, :add, fn _, _ -> 3 end)
Mimic.Server.verify_on_exit(self())
expect(Calculator, :add, fn _, _ -> 3 end)
send(parent_pid, {:ok, self()})
end)

assert_receive({:ok, child_pid})

assert_raise Mimic.VerificationError, fn ->
verify!(child_pid)
end
end
end
end

0 comments on commit 5211b68

Please sign in to comment.