Skip to content

Commit

Permalink
Add failing "mocks over-called" verify!/1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
metavida committed Aug 4, 2023
1 parent ca2b822 commit 617a470
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions test/mox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,11 @@ defmodule MoxTest do
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
assert_raise Mox.VerificationError, message, &verify!/0

task =
Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
CalcMock.add(2, 3)
CalcMock.add(4, 5)
end)

Task.yield(task)
Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
CalcMock.add(2, 3)
CalcMock.add(4, 5)
end)
|> Task.yield()

message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
assert_raise Mox.VerificationError, message, &verify!/0
Expand Down Expand Up @@ -500,6 +498,25 @@ defmodule MoxTest do
verify!(SciCalcOnlyMock)
end

test "verifies when mocks are over-called in the process in private mode" do
set_mox_private()

verify!(CalcMock)
expect(CalcMock, :add, 1, fn x, y -> x + y end)
expect(SciCalcOnlyMock, :exponent, fn x, y -> x * y end)

# Emulate mock calls within code that has aggressive error handling
try do
CalcMock.add(1, 2)
CalcMock.add(3, 4)
catch _, _ ->
:ok
end

message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
end

test "verifies all mocks for current process in global mode" do
set_mox_global()

Expand Down Expand Up @@ -529,6 +546,26 @@ defmodule MoxTest do
verify!(SciCalcOnlyMock)
end

test "verifies mocks are over-called for the current process in global mode" do
set_mox_global()

verify!()
expect(CalcMock, :add, 1, fn x, y -> x + y end)
expect(SciCalcOnlyMock, :exponent, fn x, y -> x * y end)

message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end

Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
CalcMock.add(2, 3)
CalcMock.add(4, 5)
end)
|> Task.yield

message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
end

test "raises if a non-mock is given" do
assert_raise ArgumentError, ~r"could not load module Unknown", fn ->
verify!(Unknown)
Expand Down

0 comments on commit 617a470

Please sign in to comment.