jrhite

jrhite

Resetting Mox mock after a single test

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?

Most Liked

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.

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).

jrhite

jrhite

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.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

We're in Beta

About us Mission Statement