I am currently struggling to handle config values in mix (particularly when running tests). This is my scenario:
- i have a client library, with some common config values (key, secret, region).
- i want to test what happens when there's no region value setup
- i have no
test.exs
file in/config
I'm currently doing it like this (and this doesn't work). Module being tested (simplified):
defmodule Streamex.Client do
@api_region Application.get_env(:streamex, :region)
@api_key Application.get_env(:streamex, :key)
@api_secret Application.get_env(:streamex, :secret)
@api_version "v1.0"
@api_url "api.getstream.io/api"
def full_url(%Request{} = r) do
url = <<"?api_key=", @api_key :: binary>>
end
end
Test:
setup_all do
Streamex.start
Application.put_env :streamex, :key, "KEY"
Application.put_env :streamex, :secret, "SECRET"
Application.put_env :streamex, :secret, ""
end
What happens when running mix test
is that the main module, which sets attributes from those values, throws the following error since it can't find valid values:
lib/streamex/client.ex:36: invalid literal nil in <<>>
I'm still starting so this may seem obvious, but i can't find a solution after reading the docs.