Background
I am trying to create a mock for one of my behaviours. To this end I am using Hammox (a variation of Mox which extends the api).
I have set my application so it gets which module to use from the configuration files, in this case config/test.exs
:
config/test.exs
config :my_app, storage: StorageMock
And this is the test file:
test/my_app/application_test.exs
defmodule MyApp.ApplicationTest do
@moduledoc false
use ExUnit.Case, async: false
use MyApp.ConnCase
import Hammox
test "client can handle an error response", %{conn: conn} do
Hammox.defmock(StorageMock, for: MyApp.Storage)
expect(StorageMock, :get, fn args ->
assert args == "Chicago"
# here we decide what the mock returns
{:ok, %{body: "Some html with weather data"}}
end)
get(conn, "~p/api/users/validate")
end
end
I am trying to test the controller’s endpoint,and I am basically mixing the docs of Hammox (GitHub - msz/hammox: 🏝 automated contract testing via type checking for Elixir functions and mocks) with what i can grasp from a Testing controllers guide (Testing Controllers — Phoenix v1.7.12).
Problem
Unfortunately, I think my setup is incorrect, as I get this error:
Compiling 1 file (.ex)
** (Mix) Could not start application my_app: exited in: MyApp.Application.start(:normal, [])
** (EXIT) an exception was raised:
** (ArgumentError) The module StorageMock was given as a child to a supervisor but it does not exist
(elixir 1.16.0) lib/supervisor.ex:797: Supervisor.init_child/1
(elixir 1.16.0) lib/enum.ex:1700: Enum."-map/2-lists^map/1-1-"/2
(elixir 1.16.0) lib/enum.ex:1700: Enum."-map/2-lists^map/1-1-"/2
(elixir 1.16.0) lib/supervisor.ex:783: Supervisor.init/2
(elixir 1.16.0) lib/supervisor.ex:707: Supervisor.start_link/2
(kernel 9.2) application_master.erl:293: :application_master.start_it_old/4
What am I missing here?
I assume there is some configuration somewhere to tell the VM that StorageMock
is something from the library, but I can’t find it.