Hello
I’m trying to mock a module using the Mox package. Inside that module I have a behaviour defined:
@callback add(number(), number(), [number()]) :: number
As well as implementation for that behaviour:
def add(a, b, rest \\ []), do: [a, b | rest] |> Enum.sum
Now I’m trying to mock the add/3
function which actually can be called as it would be add/2
(which I’m actually doing inside the “app” code:
@special_module.add(2, 3)
I tried different expect
s inside the test like:
defp xxx(_, _, _ \\ []), do: 6
test "adds two numbers" do
Test.SpecialModuleMock
|> expect(:add, &xxx/3)
assert Myapp.hello() == 6
end
but none of my ideas works… as the code tried to call add/2
this is exactly the error I’m getting:
1) test adds two numbers (MyappTest)
test/myapp_test.exs:9
** (UndefinedFunctionError) function Test.SpecialModuleMock.add/2 is undefined or private. Did you mean:
* add/3
code: assert Myapp.hello() == 6
stacktrace:
Test.SpecialModuleMock.add(2, 3)
test/myapp_test.exs:13: (test)
I created a repository where I have a sample mocked module that does not have optional arguments:
Then I added 3rd argument, and it’s failing:
It looks like the cleanest way to deal with this issue would be to be able to define optional arguments inside the @callback
attribute(behaviour defined with optional args) - I couldn’t find a way to do it? Is that even possible?
Otherwise, I’m not sure how to write an expect
function so that the Mox will honour optionality of 3rd argument?
I can’t be the first person to mock a function with optional arguments I surely need to be missing something simple?