Skip to content

Commit

Permalink
Improve verify/0 and verify/1 test coverage (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
metavida authored Aug 10, 2023
1 parent a59a184 commit 0e5a259
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 31 deletions.
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)

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

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

0 comments on commit 0e5a259

Please sign in to comment.