Fl4m3Ph03n1x

Fl4m3Ph03n1x

Mox mocks not working for other processes

Background

I have a small app where I have a pool that creates several workers. The pool acts as a supervisor for said workers.

Both (the pool and the workers) have a dependency on another module, the ExRegistry. This module registers any process that provides a key.

Test

While using Mox I want to make sure the worker is doing its job correctly, I don’t really want to test the registry just yet. So I stub the RegistryMock with the real one:

stub(RegistryMock, :via_tuple, &ExRegistry.via_tuple/1)
{:ok, _pid} = Worker.start_link({1})

Problem

The problem here is that the pool fails to start:

 Could not start application mox_issue: MoxIssue.Application.start(:normal, []) returned an error: shutdown: failed to start child: MoxIssue.Pool
    ** (EXIT) an exception was raised:
        ** (Mox.UnexpectedCallError) no expectation defined for MoxIssue.RegistryMock.via_tuple/1 in process #PID<0.163.0>

I think this happens because the pool is another process, which also needs access to the RegistryMock but Mox fails to give it access. To test this I set the options on Mox for global so all processes can use the mocks, but it still fails with the same error.

Question

How does Mox behave with multiple processes requiring the same Mock?

Most Liked

ColmB

ColmB

Sorry to drag this thread back up, but it is now the #1 hit for the UnexpectedCallError in the OP. Just to note that beyond the more esoteric situations discussed in the thread so far, you can also see this error via a simple race condition. This situation is discussed in the doco under Blocking on Expectations at Mox — Mox v1.0.1. It also comes up a few times in the closed Mox issues. Basically if the test finishes before any spawned processes then Mox will tear down the expectations before the tested behaviour uses it. Can be very confusing as you may partial mock hits where it is set to expect multiple hits. One way around it is to force the test to wait by messaging from within the mock. From that doco:

test "calling a mock from a different process" do
  parent = self()
  ref = make_ref()

  expect(MyApp.MockWeatherAPI, :temp, fn _loc ->
    send(parent, {ref, :temp})
    {:ok, 30}
  end)

  spawn(fn -> MyApp.HumanizedWeather.temp({50.06, 19.94}) end)

  assert_receive {^ref, :temp}

  verify!()
end

Hope that helps someone in a similar situation!

peerreynders

peerreynders

This approach seems to work (when setup :set_mox_global doesn’t):

defmodule MockShare do
  import Mox

  def init(args) do
    MyApp.CalcMock
    |> expect(:add, fn x, y -> x + y end)
    |> expect(:mult, fn x, y -> x * y end)

    {:ok, args}
  end

  def handle_call({:share, pid}, _from, state) do
    Mox.allow(MyApp.CalcMock, self(), pid)
    {:reply, :ok, state}
  end

  # ---

  def child_spec(_args),
    do: %{
      id: __MODULE__,
      start: {__MODULE__, :start_link, []},
      restart: :permanent,
      shutdown: 5000,
      type: :worker
    }

  def start_link(),
    do: GenServer.start_link(__MODULE__, [])

  def share(share_id, pid),
    do: GenServer.call(share_id, {:share, pid})
end

defmodule MyAppTest do
  use ExUnit.Case, async: true

  import Mox

  def share_mock(_context) do
    {:ok, share_pid} = start_supervised(MockShare)

    [share_pid: share_pid]
  end

  setup_all :share_mock
  setup :verify_on_exit!

  test "Use add expectation in task", context do
    MockShare.share(context.share_pid, self())

    task = Task.async(fn -> MyApp.CalcMock.add(2, 3) end)
    result = Task.await(task)
    assert result == 5
  end

  test "Use mult expectation in task", context do
    MockShare.share(context.share_pid, self())

    task = Task.async(fn -> MyApp.CalcMock.mult(2, 3) end)
    result = Task.await(task)
    assert result == 6
  end
end

Mox.allow/3


The only way I could get global mode to work:

defmodule MyAppTest do
  use ExUnit.Case

  import Mox

  # setup :set_mox_global - doesn't work

  setup_all do
    expect(MyApp.CalcMock, :add, fn x, y -> x + y end)
    expect(MyApp.CalcMock, :mult, fn x, y -> x * y end)
    set_mox_global()
    %{}
  end

  test "Use add expectation in task" do
    task = Task.async(fn -> MyApp.CalcMock.add(2, 3) end)
    result = Task.await(task)
    assert result == 5
  end

  test "Use mult expectation in task" do
    task = Task.async(fn -> MyApp.CalcMock.mult(2, 3) end)
    result = Task.await(task)
    assert result == 6
  end
end

The way I understand the documentation this should only work in global mode …

defmodule MyAppTest do
  use ExUnit.Case, async: true

  import Mox

  setup [:set_mox_private, :verify_on_exit!]

  def add,
    do: MyApp.CalcMock.add(2, 3)

  def mult,
    do: MyApp.CalcMock.mult(2, 3)

  test "Use add expectation in task" do
    expect(MyApp.CalcMock, :add, fn x, y -> x + y end)
    task = Task.async(&add/0)
    result = Task.await(task)
    assert result == 5
  end

  test "Use mult expectation in task" do
    expect(MyApp.CalcMock, :mult, fn x, y -> x * y end)
    task = Task.async(&mult/0)
    result = Task.await(task)
    assert result == 6
  end
end

and yet the task process has no problem using the mocked functions.

al2o3cr

al2o3cr

There’s a note about this below the example - on Elixir 1.8+ Mox can use $callers to detect when a parent process has an expectation set. (see also the similar machinery in DBConnection)

That’s not going to help for your case, though, since the test process and the registry aren’t related in a parent->child sense.

hubertlepicki

hubertlepicki

I don’t know if you should be using it like that in first place. You are attempting to mock the piece of infrastructure you are in control, and mocking/stubbing in my mind should be reserved to dealing with elements of infrastructure you don’t have control over: like remote services/endpoints, systems talking to external systems etc.

If you don’t want to touch ExRegistry in your tests, you could write a testing module that does something similar that makes sense in your test and inject it in it’s place for testing purposes, or isolate the worker further and test it in isolation.

peerreynders

peerreynders

A quick scan of the source code suggests to me that expectations are registered by process id - if that is true the same expectation would have to registered by/for the pool process.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement