Fl4m3Ph03n1x
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 · GitHub) with what i can grasp from a Testing controllers guide (Testing Controllers — Phoenix v1.8.8).
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #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
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
rio517
I am not very familiar with them, but looking at the Mox library and hammox, I don’t think StorageMock is coming from the library. I cannot find any reference to it in the hammox or mox hexdocs.
According to the Mox documentation, you have to define your own custom behavior. My understanding is that you define a real behavior and a mock behavior and then switch to the mock during tests. A good example is in the readme for mox.
https://github.com/dashbitco/mox?tab=readme-ov-file#4-define-the-mock-so-it-is-used-during-tests
Fl4m3Ph03n1x
StorageMockis my equivalent ofDatabaseMockfrom this README (GitHub - msz/hammox: 🏝 automated contract testing via type checking for Elixir functions and mocks · GitHub).The behaviour is called
Storage, andStorageMockis the Mock for said behaviour.Both Hammox and Mox work the same way in this regard.
al2o3cr
Based on the error message, it seems like the application is referencing
StorageMockduring boot and crashing.That module isn’t defined until something says
Hammox.defmock(StorageMock, for: MyApp.Storage)- you may need to put that call in yourtest_helper.exsto get it to happen before application boot.LostKobrakai
test_helper.exsis also loaded after application startup. What works is putting thedefmockin a compiled file (not a script), e.g. intest/support/mocks.exor something like that. See the documentation here: Mox — Mox v1.1.0Fl4m3Ph03n1x
This is really close to the problem. Turns out my issue was that I was referencing
StorageMockat application startup, namely:By removing
StorageMockfrom the list of children (when running tests) the error was fixed.For those of you curious, you can check in which env the application is running, by using
Mix.env(). Thus, you can do something like:This is the solution I ended up going with, as it means you can make use of the
config/test.exsto define what modules you want to use. You can even change modules at runtime by usingApplication.put_env/3if you are inclined to do that, which can also come in handy when doing some specific types of tests.Thanks for the help!