Dependency/configuration woes

I have a Phoenix application (PhoenixApp) that depends on a plain Elixir application I wrote (Dependency). Dependency in turn depends on another Elixir library I wrote that wraps a third-party API (API). These are just regular dependencies; nothing is an umbrella app.

In order to test Dependency without hitting the third-party API I use Application.get_env/3 to point calls to API to a mock. So the API-calling module in Dependency looks like:

defmodule Dependency.APIUsingModule do
  @api_module Application.get_env(:dependency, :api)

  def call_the_api(arg) do
    @api_module.call(arg)
  end
end

As described above, config/dev.exs and config/prod.exs in Dependency then include the line config :dependency, :api, APIModule, and config/test.exs in Dependency has the line config :dependency, :api, MockAPIModule. All is well and I can confirm that when I run the tests for Dependency they use the mock API.

I learned the hard way that for the purposes of PhoenixApp, I need to configure these values directly in PhoenixApp, so the respective config files in PhoenixApp have the same lines. I expected that when my tests in PhoenixApp call functions in Dependency for which I have the mock API in place, the mock API would be called. Sadly I am able to confirm that when I run tests for PhoenixApp, they call the real API and not the mock.

Is this expected behavior? If so, can I accomplish what I am trying to accomplish without a major overhaul? If it is not expected behavior, I would really appreciate it if someone can point out what I am doing wrong or offer any suggestions on how to troubleshoot.

The configuration of dependencies is not used. You always need to configure them by hand from your actual project. So, yes, as far as I understand your problem, it is expected behaviour.

Thanks - I think maybe I was not clear. I understand that the configuration in my dependency is not used; my problem is that I have the configuration for the dependency specified in the config files of the actual project (not just in the config files of the dependency itself) and that doesn’t appear to be used either. That’s what is unexpected to me.

Since @api_module Application.get_env(:dependency, :api) will be set at compile time (not runtime) its possible that the compiled artifacts still have the production module name.

You could try either of:

MIX_ENV=test mix deps.compile dependency --force

before you run tests just to ensure you have the expected configuration built in, Or adjust your module to make this a runtime config. I think this would be the preferred option:

defmodule Dependency.APIUsingModule do
  def call_the_api(arg, module \\ Application.get_env(:dependency, :api)) do
    module.call(arg)
  end
end

The retrieval of the module name (which is from ETS given the implementation of Application.get_env/2) will be minimal compared to an API call.

2 Likes

mix compile --force will NOT recompile dependencies, which is what he needs to do. He would need to run MIX_ENV=test mix deps.compile dependency --force

1 Like

Of course you are right and I fixed my reply accordingly.

Thanks! This morning I was thinking it might be something like that but I could not figure out how to force it to recompile in test. That did the trick. I also like the alternative, which seems preferable to me also in the long run.