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

Conversation

metavida
Copy link
Contributor

@metavida metavida commented Aug 7, 2023

Before this PR the tests of verify/0 didn't assert that failures would be reported across multiple Mocks and tests for verify/1 didn't assert that failures in other Mocks would be ignored.

...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)
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.

Comment on lines -358 to +365
assert CalcMock.add(2, 3) == 5
CalcMock.add(2, 3)
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.

Comment on lines 356 to 363
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

...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.
Comment on lines -397 to +416
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"
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: 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.

@josevalim josevalim merged commit 0e5a259 into dashbitco:master Aug 10, 2023
0 of 2 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@metavida metavida deleted the improve-veriy/1-test-coverage branch August 10, 2023 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants