jrhite

jrhite

I’m new to elixir and am seeing confusing/undesirable behavior when using Mox.

I just want to use a mock for a single test module. Let’s say I have 2 tests. Here is sample code:

defmodule MyTest do
  setup_all do
    defmock(DateTimeMock, for: DateTimeApi)

    :ok
  end

  test "test1" do
    {:ok, expected_datetime, _} = DateTime.from_iso8601("2019-09-08T00:00:00.000000Z")
    expect(DateTimeMock, :utc_now, fn _ -> expected_datetime end)
  end

  test "test2" do
    expect(something else)
  end
end 

defmodule MyTest2 do
  setup_all do
    defmock(DateTimeMock, for: DateTimeApi)

    :ok
  end

  test "test1" do
  end

  test "test2" do
  end
end  

When MyTest2 runs I will see the error: (Mox.UnexpectedCallError) no expectation defined

Defining a mock for a single test ‘leaks’ out and affects all tests.

Does Mox have a way to revert the mocked module back to the original module after the test has finished?

Showing Posts 1 to 9

al2o3cr

al2o3cr

Your example doesn’t show how DateTimeMock is used - how is code in MyTest2 interacting with it?

For instance, do you have the mock set in application config? In that case EVERY test will hit it. stub might be the way out.

jrhite

jrhite OP

In my case I’m assuming I’m working with a legacy code introducing Mox. So MyTest2 doesn’t have any expects. Thus it fails. Yeah I’ve been able to use stub_with to get around that issue, but it feels very hacky.

LostKobrakai

LostKobrakai

There are a few things. Mock modules of Mox are empty shells. Without expectations/stubs they don’t know what to do/return when being called. Therefore there’s a technical requirement to setting up expectations/stubs.

The other part is that you don’t want to have tests, which do not tell you about the usage of a mock. Without expectations/stubs being setup in a testcase any other developer might think the same code is run as is in production, which is not true.

If you don’t want to have to setup stubs/expectations for your mock “everywhere” the best solution is to not use the mock for those testcases, but e.g. a module with a dummy implementation or even the prod implementation.

Aside:
You should put your defmock calls in test_helpers.exs and not in your setup functions. defmock creates a module and modules are always global to the beam instance. You cannot scope those to a single test unless you use different names.

jrhite

jrhite OP

That’s just the info I was looking for. Thank you.

I put the defmock in the test module itself instead of test_helpers.exs as an attempt to scope the mock to this test only.

I was was hoping there might be a function like JS Jest’s mockRestore. After a test it restores the mocked module back to the original module implementation. This allows for easy mock integration into a legacy system where it’s not feasible to update all tests at once. It seems like, in theory, Mox should be able to store the original module away in the Mox Genserver and restore it after the test exits.

Possible good feature request?

jrhite

jrhite OP

And yeah, I was thinking about just using stub_with() with the prod implementation for the other tests that have not yet been upgraded to use, but restoring the mock to its original implementation after a test seems like better test to test isolation.

al2o3cr

al2o3cr

Mox doesn’t have this feature because that’s not how it works - somewhere in your test setup / config, code is storing DateTimeMock in a spot where production stores DateTimeApi. The docs show two possible forms, but your code may do something different:

config :my_app, :calculator, MyApp.CalcMock
# or
Application.put_env(:my_app, :calculator, MyApp.CalcMock)

What you’re describing is closer to the behavior of tools like :meck which manipulate module resolution at runtime. That comes with a whole new set of weird behaviors (for instance, code coverage can fail unexpectedly).

ityonemo

ityonemo

You don’t need to do that. The tests can run concurrently and you never know what order they will run in because order is randomized. Every test lives in its own beam process. Mock often can see what process was responsible for what chain of events and so if you do it right, changes you make to the mocks are contained to your test process and its friends (for some value of “friends”) and the whole universe of that testing setup is thrown away at the end of the test.

This is what makes elixir testing awesome, btw, When you get really good you can run all of your tests (even integration tests that escape the BEAM) concurrently without stomping on each other’s view of what the VM should look like.

jrhite

jrhite OP

I see, thanks for the explanation.

I think the best solution in my case would be to create the Mox mock in test_helper and then also use stub_with using the real implementation for tests that haven’t set expectations yet. Then the team can change the tests over on a test-by-test basis.

woohaaha

woohaaha

Do you have any examples of how this is setup for you? (regarding unit/integration tests). Would love to see some examples.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
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
achenet
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
kpanic
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
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
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
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
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
KristerV
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews