A simple macro to allow switching alias statements out with different values based upon environment.
Available in Hex, the package can be installed as:
Add stub_alias to your list of dependencies in mix.exs:
def deps do
[{:stub_alias, "~> 0.1.0"}]
end
StubAlias is a compile time dependency and can be left out of the applications list.
In your code you may have some alias statements like the following:
EXAMPLE
defmodule MyModule do
alias MyModule.Foo
def stuff() do
Foo.do_something_with_side_effects
end
endObviously, during testing, it makes it more difficult to test the function stuff/0 since it calls another function which has undesierable side effects (or requires some system state like a running GenServer).
If you add to your config/test.exs configuration like the following:
config :stub_alias,
"MyModule.Foo": MyModule.Stubs.FooSetup your mix.exs to compile in the test/support folder like in Phoenix. Then replace alias MyModule.Foo with stub_alias MyModule.Foo (after import StubAlias of course):
SOLUTION
lib/my_module.ex
defmodule MyModule do
import StubAlias
stub_alias MyModule.Foo
def stuff() do
Foo.do_something_with_side_effects
end
endtest/support/foo.ex
defmodule MyModule.Stubs.Foo do
def do_something_with_side_effects() do
# Return hard coded data or get data from an agent or whatever you please
results = %{}
results
end
endIn the :test Mix.env, your aliases will be replaced as desired. This then allows you to have a compiled test/support folder which supplies those stubs, making easy explicit replacements of code at test time.