Skip to content

Commit 617a470

Browse files
committed
Add failing "mocks over-called" verify!/1 tests
1 parent ca2b822 commit 617a470

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

test/mox_test.exs

+44-7
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,11 @@ defmodule MoxTest do
458458
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
459459
assert_raise Mox.VerificationError, message, &verify!/0
460460

461-
task =
462-
Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
463-
CalcMock.add(2, 3)
464-
CalcMock.add(4, 5)
465-
end)
466-
467-
Task.yield(task)
461+
Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
462+
CalcMock.add(2, 3)
463+
CalcMock.add(4, 5)
464+
end)
465+
|> Task.yield()
468466

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

501+
test "verifies when mocks are over-called in the process in private mode" do
502+
set_mox_private()
503+
504+
verify!(CalcMock)
505+
expect(CalcMock, :add, 1, fn x, y -> x + y end)
506+
expect(SciCalcOnlyMock, :exponent, fn x, y -> x * y end)
507+
508+
# Emulate mock calls within code that has aggressive error handling
509+
try do
510+
CalcMock.add(1, 2)
511+
CalcMock.add(3, 4)
512+
catch _, _ ->
513+
:ok
514+
end
515+
516+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
517+
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
518+
end
519+
503520
test "verifies all mocks for current process in global mode" do
504521
set_mox_global()
505522

@@ -529,6 +546,26 @@ defmodule MoxTest do
529546
verify!(SciCalcOnlyMock)
530547
end
531548

549+
test "verifies mocks are over-called for the current process in global mode" do
550+
set_mox_global()
551+
552+
verify!()
553+
expect(CalcMock, :add, 1, fn x, y -> x + y end)
554+
expect(SciCalcOnlyMock, :exponent, fn x, y -> x * y end)
555+
556+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
557+
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
558+
559+
Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
560+
CalcMock.add(2, 3)
561+
CalcMock.add(4, 5)
562+
end)
563+
|> Task.yield
564+
565+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
566+
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
567+
end
568+
532569
test "raises if a non-mock is given" do
533570
assert_raise ArgumentError, ~r"could not load module Unknown", fn ->
534571
verify!(Unknown)

0 commit comments

Comments
 (0)