It appears I am doing something off, as after mocking a module using mock, the module is completely unloaded from memory and subsequent code that depends on the module being there fails.
As a working proof of concept, the following script fails - the error is raised every time execution gets to run the last line, e.g. MyModule3.my_fun(1):
Mix.install [:mock]
defmodule MyModule1 do
def my_fun(arg) do
arg + 1
end
end
defmodule MyModule2 do
import Mock
def my_fun(_arg) do
with_mock(MyModule1, my_fun: fn _number -> 123 end) do
MyModule1.my_fun(1)
end
end
end
MyModule1.my_fun(1) |> IO.inspect
MyModule2.my_fun(1) |> IO.inspect
MyModule1.my_fun(1) |> IO.inspect
The failure is:
$ elixir example.exs
2
123
** (UndefinedFunctionError) function MyModule1.my_fun/1 is undefined (module MyModule1 is not available)
MyModule1.my_fun(1)
example.exs:27: (file)
(elixir 1.14.1) lib/code.ex:1245: Code.require_file/2
$
After digging a little, the mocking code appears to be reasonably straightforward, which leaves me quite puzzled as to what’s going on:
https://github.com/jjh42/mock/blob/f3580cb68692b41ee59e9784ba253473dab9420d/lib/mock.ex#L340-L368
Trivia:
elixir --version
Erlang/OTP 25 [erts-13.1.1] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]
Elixir 1.14.1 (compiled with Erlang/OTP 25)






















