Module not available in mock do block
JamieREvans opened this issue · 1 comments
JamieREvans commented
I have a custom module MyApp.MyModule
with a function do_something
in a compiled file (my_module.ex).
defmodule MyApp.MyModule do
def do_something(the_something), do: "Doing #{the_something}"
end
And I have a test that is trying to mock the function and see that it was called, but I'm getting an error saying that the module is not available.
defmodule MyApp.MyModuleTest do
import Mock
alias MyApp.MyModule
describe "see if I can mock" do
test "Try mocking" do
assert MyModule.do_something("a thing") == "Doing a thing")
with_mock(MyModule, [do_something: fn(_the_something) -> "Something else" end]) do
assert MyModule.do_something("another thing") == "Something else"
end
end
end
end
And the error is:
** (UndefinedFunctionError) function MyApp.MyModule.do_something/1 is undefined (module MyApp.MyModule is not available)
code: MyApp.MyModule.do_something("another thing")
stacktrace:
(my_app) MyApp.MyModule.do_something("another thing")
test/my_app/my_module_tests.exs:10: (test)
As you can see, I can call the function perfectly fine outside of the mock, but if I can't call it inside the mock, it defeats the entire purpose of the mocking tool.
Olshansk commented
I tried running your test and it passed for me.
There were two tiny syntax changes I had to make:
- Remove closing
)
on line 8 - Add
use ExUnit.Case
defmodule MyApp.MyModuleTest do
use ExUnit.Case
import Mock
alias MyApp.MyModule
describe "see if I can mock" do
test "Try mocking" do
assert MyModule.do_something("a thing") == "Doing a thing"
with_mock(MyModule, [do_something: fn(_the_something) -> "Something else" end]) do
assert MyModule.do_something("another thing") == "Something else"
end
end
end
end
Could you perhaps push a branch where I could try to reproduce it?