srowley

srowley

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.

Showing Posts 1 to 6

NobbZ

NobbZ

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.

srowley

srowley OP

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.

kip

kip

ex_cldr Core Team

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.

jeremyjh

jeremyjh

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

kip

kip

ex_cldr Core Team

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

srowley

srowley OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews