Skip to content

Commit

Permalink
Add failing "mocks are over-called" tests
Browse files Browse the repository at this point in the history
  • Loading branch information
metavida committed Aug 4, 2023
1 parent a59a184 commit ada67ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/mox/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ defmodule Mox.Application do
use Application

def start(_, _) do
children = [Mox.Server]
children = [
Mox.Server,
{Task.Supervisor, name: MoxTests.TaskSupervisor},
]
Supervisor.start_link(children, name: Mox.Supervisor, strategy: :one_for_one)
end
end
39 changes: 39 additions & 0 deletions test/mox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ defmodule MoxTest do
assert_raise Mox.VerificationError, message, &verify!/0
end

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

verify!()
expect(CalcMock, :add, 1, 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, &verify!/0
end

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

Expand All @@ -385,6 +403,27 @@ defmodule MoxTest do
message = ~r"expected CalcMock.add/2 to be invoked 2 times but it was invoked once"
assert_raise Mox.VerificationError, message, &verify!/0
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)

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)

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

describe "verify!/1" do
Expand Down

0 comments on commit ada67ca

Please sign in to comment.