gmile
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)
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
mock_modulesisn’t intended to be used outside that module, because it doesn’t completely un-mock things - all the places inMockthat callmock_moduleseventually either invoke:meck.unload/0or:meck.unload/1like this:https://github.com/jjh42/mock/blob/f3580cb68692b41ee59e9784ba253473dab9420d/lib/mock.ex#L324-L336
gmile
Sorry if a kindergarten question, but how would I use Mock in a way that un-mocks mocked modules then?
Likely mistakenly, I was under an impression that mocking is only in effect in the scope of
with_mock’s being/end block.Expecting that perhaps unloading
meckexplicitly will solve the problem (e.g. bring back modules to their original un-mocked state), I just tried adding:meck.unload()right before the problematic line - this didn’t helped, the problem is still there:Btw, I only mentioned
mock_modulesin my original module because,with_mockused in my example eventually boils down to callingmock_modules.al2o3cr
:meck.unloaddoesn’t “put the module back”, it unloads it entirely (from the docs):Future calls like
MyModule1.my_funwill try to loadMyModule1from disk, but if it’s not compiled it won’t be found at all.For instance, I made your example pass by extracting
MyModule1toon_disk.ex(the name is arbitrary, but theexis important):and then compiling it with
elixirc on_disk.ex.Then running the remainder as
elixir example.exsprints out:as expected.
gmile
@al2o3cr thank you, your notes were instrumental in helping us understand what was going on with mocking in our case!
While my example above ended up being overly simplified, our actual situation turned out to be much more involved. I will describe it below, but a word of warning: it will sound like it’s not related to what has been so far discussed in this thread
We were mocking something in a test, and seeing the whole test suit stop working as a consequence of that. A suite would throw a cryptic error saying that such and such process name cannot be located.
It turned out that, at the time of mocking the specific module (and as you pointed out, that modules’ code being completely unloaded from memory), code from that module also happened to be running in the background (which is by design).
We were mocking a call to
:hackney.request, and there was some code in the background that was executing a long-running:hackney.request. Our module happens to be a part of app supervision tree, so unloading:hackneyfrom memory resulted in taking down entire app (e.g. during test suite run) with it.Conclusion: take great care if you absolutely must use Mock library. Avoid it altogether if you can.