softodrom
Change config compile env variables during ExUnit test
Hi,
I have some compile env and I want to change their values in some specific test cases and then change them back.
Is there any way of doing that? recompile/1 is not working as this function simply recompiles Elixir modules, without reloading configuration, recompiling dependencies, or restarting applications.
I think that I need to reload the configuration too.
Best regards.
Most Liked
LostKobrakai
For libraries you should avoid compile time config and even using the app env:
https://hexdocs.pm/elixir/library-guidelines.html#avoid-application-configuration
KristerV
one option is to have separate config files, but then you would have to run with MIX_ENV=test_this mix test. does this solve it?
but what are you testing exactly? why are compile time variables changing?
i’m pretty sure you don’t need to test if compile time variables work, that’s elixir. you can trust elixir. you probably want to test what happens in your code with different values for that variable. in that case just hardcode the variable into the test. meaning ignore the env var and create a new one, then pass it into the function/process you’re testing.
if it turns out that your code uses the env var in the function you’re testing, well then make the value an argument in the function so the test can pass it in (which is usually a good idea in terms of good code anyway).
Hermanverschooten
Why not create a validation function for the config, and test that?
Then maybe compilation could fail when something invalid is given.







