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 all commits
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
108 changes: 77 additions & 31 deletions test/mox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -351,39 +351,54 @@ 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
error = assert_raise(Mox.VerificationError, &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"

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.

error = assert_raise(Mox.VerificationError, &verify!/0)
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"

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)
error = assert_raise(Mox.VerificationError, &verify!/0)
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

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
error = assert_raise(Mox.VerificationError, &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"

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

Task.await(task)
error = assert_raise(Mox.VerificationError, &verify!/0)
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"

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)
error = assert_raise(Mox.VerificationError, &verify!/0)
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

Expand All @@ -392,40 +407,71 @@ 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
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"
Comment on lines -397 to +416
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.


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

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

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

expect(CalcMock, :add, fn x, y -> x + y end)
error = assert_raise Mox.VerificationError, fn -> verify!(CalcMock) end
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"
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
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"

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

Task.await(task)
Task.async(fn -> CalcMock.add(2, 3) end)
|> Task.await()
verify!(CalcMock)

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

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

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

error = assert_raise(Mox.VerificationError, &verify!/0)
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"

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
Loading