Background
I am trying to use Mox to replicate some simple interactions but I am failing test that should be passing (or so I think):
Code
After creating a basic hello world project I modified to the following:
defmodule Testmox do
alias MyApp.Calculator.Impl, as: Calc
def hello, do: "Hello #{inspect Calc.add(1, 1)}"
def bye, do: "Bye #{inspect Calc.mult(2, 2)}"
end
Textmox
uses a basic calculator module:
defmodule MyApp.Calculator do
@callback add(integer(), integer()) :: integer()
@callback mult(integer(), integer()) :: integer()
end
defmodule MyApp.Calculator.Impl do
@behaviour MyApp.Calculator
alias MyApp.Calculator
@impl Calculator
def add(x, y), do: x + y
@impl Calculator
def mult(x, y), do: x * y
end
As you see, nothing extraordinary here. Now onto the test!
defmodule CalculatorTest do
use ExUnit.Case, async: true
alias Testmox
alias MyApp.Calculator.Impl, as: Calc
import Mox
# Make sure mocks are verified when the test exits
setup :set_mox_global
setup :verify_on_exit!
test "invokes add and mult" do
MyApp.CalcMock
|> expect(:add, &Calc.add/2)
|> expect(:mult, &Calc.mult/2)
Testmox.hello()
Testmox.bye()
end
end
Problem?
This test fails, even though Calc.add
and Calc.mult
are being called in the hello
and bye
functions respectively. I have even set the mode to global, so other processes can use it, but it still fails the tests. What am I doing wrong?
EDIT: I have also changed test_helpers.ex
:
ExUnit.start()
Mox.defmock(MyApp.CalcMock, for: MyApp.Calculator)