Mox return a UnexpectedCallError

Hi,
I’m trying to test my config settings.
To do that, i use a module that load them: like this

defmodule App.SettingsBehaviour do
  @moduledoc """
  Define the behavior for accessing environment variable settings
  """
  @callback is_search_enabled :: boolean
end

defmodule App.BoundSettings do
  @options Application.get_env(:app, :options, App.Settings)

  def is_search_enabled do
    @options.is_search_enabled
  end
end

defmodule App.Settings do
  @behaviour App.SettingsBehaviour

  @impl App.SettingsBehaviour
  def is_search_enabled, do: Application.get_env(:app, :search_enabled, false)
end

config/test.exs

config :app, :options, App.OptionsMock
config :app, search_enabled: true

I have a liveview controller calling App.Settings, and when i try to mock it it returns UnexpectedCallError. Does anyone understand what could fail here?

it seems that you have 2 implementations of the module App.Settings, one doesn’t define the behaviour App.SettingsBehaviour. one might be overwriting the other.

also, are you defining the mock in your test_helper.exs file?
https://hexdocs.pm/mox/Mox.html#defmock/2

Sorry, the module name copy was a typo of my part. I corrected it.
Yes i am defining the mocks in supports/mocks.ex!!

how is your config for search_enabled in the test config file

I updated my post with the config/test.exs content

  • App.Settings doesn’t call any function, just get a value from your Application config store.
  • App.BoundSettings doesn’t implement the behaviour.
  • UnexpectedCallError usually means that you’re calling the the mock module without setting any expectation on it.

given the context you provided, code paths that use AppBoundSettings doesn’t have a expectation on App.OptionsMock.

https://hexdocs.pm/mox/Mox.html#expect/4

One of the expectation was missing making load of tests fails