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.






















