Skip to content

Commit f7de7fc

Browse files
committed
Add failing "mocks over-called" verify!/1 tests
1 parent ada67ca commit f7de7fc

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

test/mox_test.exs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,11 @@ defmodule MoxTest do
413413
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
414414
assert_raise Mox.VerificationError, message, &verify!/0
415415

416-
task =
417-
Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
418-
CalcMock.add(2, 3)
419-
CalcMock.add(4, 5)
420-
end)
421-
422-
Task.yield(task)
416+
Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
417+
CalcMock.add(2, 3)
418+
CalcMock.add(4, 5)
419+
end)
420+
|> Task.yield()
423421

424422
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
425423
assert_raise Mox.VerificationError, message, &verify!/0
@@ -444,6 +442,25 @@ defmodule MoxTest do
444442
assert_raise Mox.VerificationError, message, &verify!/0
445443
end
446444

445+
test "verifies when mocks are over-called in the process in private mode" do
446+
set_mox_private()
447+
448+
verify!(CalcMock)
449+
expect(CalcMock, :add, 1, fn x, y -> x + y end)
450+
expect(SciCalcOnlyMock, :exponent, fn x, y -> x * y end)
451+
452+
# Emulate mock calls within code that has aggressive error handling
453+
try do
454+
CalcMock.add(1, 2)
455+
CalcMock.add(3, 4)
456+
catch _, _ ->
457+
:ok
458+
end
459+
460+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
461+
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
462+
end
463+
447464
test "verifies all mocks for current process in global mode" do
448465
set_mox_global()
449466

@@ -467,6 +484,26 @@ defmodule MoxTest do
467484
assert_raise Mox.VerificationError, message, &verify!/0
468485
end
469486

487+
test "verifies mocks are over-called for the current process in global mode" do
488+
set_mox_global()
489+
490+
verify!()
491+
expect(CalcMock, :add, 1, fn x, y -> x + y end)
492+
expect(SciCalcOnlyMock, :exponent, fn x, y -> x * y end)
493+
494+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
495+
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
496+
497+
Task.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn ->
498+
CalcMock.add(2, 3)
499+
CalcMock.add(4, 5)
500+
end)
501+
|> Task.yield
502+
503+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
504+
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
505+
end
506+
470507
test "raises if a non-mock is given" do
471508
assert_raise ArgumentError, ~r"could not load module Unknown", fn ->
472509
verify!(Unknown)

0 commit comments

Comments
 (0)