Skip to content

Commit ada67ca

Browse files
committed
Add failing "mocks are over-called" tests
1 parent a59a184 commit ada67ca

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/mox/application.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ defmodule Mox.Application do
44
use Application
55

66
def start(_, _) do
7-
children = [Mox.Server]
7+
children = [
8+
Mox.Server,
9+
{Task.Supervisor, name: MoxTests.TaskSupervisor},
10+
]
811
Supervisor.start_link(children, name: Mox.Supervisor, strategy: :one_for_one)
912
end
1013
end

test/mox_test.exs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,24 @@ defmodule MoxTest do
363363
assert_raise Mox.VerificationError, message, &verify!/0
364364
end
365365

366+
test "verifies when mocks are over-called in the process in private mode" do
367+
set_mox_private()
368+
369+
verify!()
370+
expect(CalcMock, :add, 1, fn x, y -> x + y end)
371+
372+
# Emulate mock calls within code that has aggressive error handling
373+
try do
374+
CalcMock.add(1, 2)
375+
CalcMock.add(3, 4)
376+
catch _, _ ->
377+
:ok
378+
end
379+
380+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
381+
assert_raise Mox.VerificationError, message, &verify!/0
382+
end
383+
366384
test "verifies all mocks for the current process in global mode" do
367385
set_mox_global()
368386

@@ -385,6 +403,27 @@ defmodule MoxTest do
385403
message = ~r"expected CalcMock.add/2 to be invoked 2 times but it was invoked once"
386404
assert_raise Mox.VerificationError, message, &verify!/0
387405
end
406+
407+
test "verifies mocks are over-called for the current process in global mode" do
408+
set_mox_global()
409+
410+
verify!()
411+
expect(CalcMock, :add, 1, fn x, y -> x + y end)
412+
413+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
414+
assert_raise Mox.VerificationError, message, &verify!/0
415+
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)
423+
424+
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 2 times"
425+
assert_raise Mox.VerificationError, message, &verify!/0
426+
end
388427
end
389428

390429
describe "verify!/1" do

0 commit comments

Comments
 (0)