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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews