-
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
Improve verify/0
and verify/1
test coverage
#142
Improve verify/0
and verify/1
test coverage
#142
Conversation
...by using more than one mock in each test.
@@ -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) |
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: 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.
assert CalcMock.add(2, 3) == 5 | ||
CalcMock.add(2, 3) |
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: 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.
test/mox_test.exs
Outdated
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 |
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.
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" |
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.
If it works, can you please apply this style in other places to clean up the tests? Thanks!
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.
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!
...by capturing the return value of `assert_raise` rather than doing a verbose try/rescue w/ assertions. This approach has the added benefit that the test will begin failing if the verify!/0 call fails to raise an exception when it should!
which then lets us perform `refute` checks on the error message in addition to asserting what the error message *does* contain.
message = ~r"expected CalcMock.add/2 to be invoked once but it was invoked 0 times" | ||
assert_raise Mox.VerificationError, message, &verify!/0 | ||
error = assert_raise(Mox.VerificationError, fn -> verify!(CalcMock) end) | ||
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" |
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: Even though the previous tests are in the describe "verify!/1"
section, they used to be calling the verify!/0
method! With this change now we're actually 1️⃣ calling the verify!/1
method and 2️⃣ asserting that the behavior changes appropriately when there are multiple mocks used within the same test case.
💚 💙 💜 💛 ❤️ |
Before this PR the tests of
verify/0
didn't assert that failures would be reported across multiple Mocks and tests forverify/1
didn't assert that failures in other Mocks would be ignored.