Skip to content
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

Improve verify/0 and verify/1 test coverage #142

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 90 additions & 28 deletions test/mox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -351,39 +351,84 @@ defmodule MoxTest do

verify!()
expect(CalcMock, :add, fn x, y -> x + y end)
expect(SciCalcOnlyMock, :exponent, fn x, y -> x * y end)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotation: I realize that this implementation is actually multiplication (not an exponent) but these tests are focused on the verify method not the expect method, so I decided not to worry about this detail.


message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
assert_raise Mox.VerificationError, message, &verify!/0
assert_raise(Mox.VerificationError, &verify!/0)
try do
verify!()
rescue
error in Mox.VerificationError ->
assert error.message =~ ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
assert error.message =~ ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_raise(Mox.VerificationError, &verify!/0)
try do
verify!()
rescue
error in Mox.VerificationError ->
assert error.message =~ ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
assert error.message =~ ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"
end
assert %Mox.VerificationError{} = exception = catch_error(&verify!/0)
assert error.message =~ ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
assert error.message =~ ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works, can you please apply this style in other places to clean up the tests? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Great call. The catch_error/1 method didn't work because it only catches assertion errors but seeing that syntax got me re-reading the documentation & I realized that assert_raise/2 explicitly "Returns the rescued exception" which also enables the style of refactor you've proposed!

Implemented the improvements in 1ee925f and 999810c


assert CalcMock.add(2, 3) == 5
CalcMock.add(2, 3)
Comment on lines -358 to +360
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotation: I remove assertions of the CalcMock.add method because, again, these tests should be focusing on assertions related to the verify method not the return behavior introduced by the expect method call.

assert_raise(Mox.VerificationError, &verify!/0)
try do
verify!()
rescue
error in Mox.VerificationError ->
refute error.message =~ ~r"expected CalcMock.add/2"
assert error.message =~ ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"
end

SciCalcOnlyMock.exponent(2, 3)
verify!()
expect(CalcMock, :add, fn x, y -> x + y end)

message = ~r"expected CalcMock.add/2 to be invoked 2 times but it was invoked once"
assert_raise Mox.VerificationError, message, &verify!/0
# Adding another expected call makes verification fail again
expect(CalcMock, :add, fn x, y -> x + y end)
assert_raise(Mox.VerificationError, &verify!/0)
try do
verify!()
rescue
error in Mox.VerificationError ->
assert error.message =~ ~r"expected CalcMock.add/2 to be invoked 2 times but it was invoked once"
refute error.message =~ ~r"expected SciCalcOnlyMock.exponent/2"
end
end

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

verify!()
expect(CalcMock, :add, 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, &verify!/0
assert_raise(Mox.VerificationError, &verify!/0)
try do
verify!()
rescue
error in Mox.VerificationError ->
assert error.message =~ ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
assert error.message =~ ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"
end

task =
Task.async(fn ->
assert CalcMock.add(2, 3) == 5
end)
Task.async(fn -> SciCalcOnlyMock.exponent(2, 4) end)
|> Task.await()

Task.await(task)
assert_raise(Mox.VerificationError, &verify!/0)
try do
verify!()
rescue
error in Mox.VerificationError ->
assert error.message =~ ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times"
refute error.message =~ ~r"expected SciCalcOnlyMock.exponent/2"
end

Task.async(fn -> CalcMock.add(5, 6) end)
|> Task.await()

verify!()
expect(CalcMock, :add, fn x, y -> x + y end)

message = ~r"expected CalcMock.add/2 to be invoked 2 times but it was invoked once"
assert_raise Mox.VerificationError, message, &verify!/0
expect(CalcMock, :add, fn x, y -> x + y end)
assert_raise(Mox.VerificationError, &verify!/0)
try do
verify!()
rescue
error in Mox.VerificationError ->
assert error.message =~ ~r"expected CalcMock.add/2 to be invoked 2 times but it was invoked once"
refute error.message =~ ~r"expected SciCalcOnlyMock.exponent/2"
end
end
end

Expand All @@ -392,40 +437,57 @@ defmodule MoxTest do
set_mox_private()

verify!(CalcMock)
verify!(SciCalcOnlyMock)
expect(CalcMock, :add, 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, &verify!/0
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
message = ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"
assert_raise Mox.VerificationError, message, fn -> verify!(SciCalcOnlyMock) end

assert CalcMock.add(2, 3) == 5
CalcMock.add(2, 3)
verify!(CalcMock)
expect(CalcMock, :add, fn x, y -> x + y end)
message = ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"
assert_raise Mox.VerificationError, message, fn -> verify!(SciCalcOnlyMock) end

SciCalcOnlyMock.exponent(2, 3)
verify!(CalcMock)
verify!(SciCalcOnlyMock)

expect(CalcMock, :add, fn x, y -> x + y end)
message = ~r"expected CalcMock.add/2 to be invoked 2 times but it was invoked once"
assert_raise Mox.VerificationError, message, &verify!/0
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
verify!(SciCalcOnlyMock)
end

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

verify!(CalcMock)
verify!(SciCalcOnlyMock)
expect(CalcMock, :add, 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, &verify!/0
assert_raise Mox.VerificationError, message, fn -> verify!(CalcMock) end
message = ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"
assert_raise Mox.VerificationError, message, fn -> verify!(SciCalcOnlyMock) end

task =
Task.async(fn ->
assert CalcMock.add(2, 3) == 5
end)

Task.await(task)
Task.async(fn -> CalcMock.add(2, 3) end)
|> Task.await()
verify!(CalcMock)
message = ~r"expected SciCalcOnlyMock.exponent/2 to be invoked once but it was invoked 0 times"
assert_raise Mox.VerificationError, message, fn -> verify!(SciCalcOnlyMock) end

SciCalcOnlyMock.exponent(2, 3)
verify!(CalcMock)
expect(CalcMock, :add, fn x, y -> x + y end)
verify!(SciCalcOnlyMock)

expect(CalcMock, :add, fn x, y -> x + y end)
message = ~r"expected CalcMock.add/2 to be invoked 2 times but it was invoked once"
assert_raise Mox.VerificationError, message, &verify!/0
verify!(SciCalcOnlyMock)
end

test "raises if a non-mock is given" do
Expand Down
1 change: 1 addition & 0 deletions test/support/mocks.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Mox.defmock(CalcMock, for: Calculator)
Mox.defmock(SciCalcOnlyMock, for: ScientificCalculator)
Mox.defmock(SciCalcMock, for: [Calculator, ScientificCalculator])

Mox.defmock(SciCalcMockWithoutOptional,
Expand Down