-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling verify!
should raise a Mox.VerificationError
if there are too many calls to a mock
#141
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
||
|
@@ -385,6 +403,26 @@ 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 | ||
start_supervised!({Task.Supervisor, name: MoxTests.TaskSupervisor}) | ||
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.Supervisor.async_nolink(MoxTests.TaskSupervisor, fn -> | ||
CalcMock.add(2, 3) | ||
CalcMock.add(4, 5) | ||
end) | ||
|> Task.yield() | ||
Comment on lines
+420
to
+421
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Annotation: Other places in this test file use a variable-based syntax instead of piping. For example: https://github.com/dashbitco/mox/blob/31217186dc7b618ea35448d2175377408a574fb3/test/mox_test.exs#L175:L181 I find this shorter, pipe-based syntax a bit nicer since it puts visual attention on the |
||
|
||
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 | ||
|
@@ -405,6 +443,25 @@ 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!(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() | ||
|
||
|
@@ -428,6 +485,27 @@ defmodule MoxTest do | |
assert_raise Mox.VerificationError, message, &verify!/0 | ||
end | ||
|
||
test "verifies mocks are over-called for the current process in global mode" do | ||
start_supervised!({Task.Supervisor, name: MoxTests.TaskSupervisor}) | ||
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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annotation: The
"verifies when mocks are over-called
tests are the tests that are currently failing & that would be useful to me in some of the code I'm working on.