AFAIK it’s not possible to store anonymous functions in the config file, but I was not able to find more info quickly.
But it’s solvable. Put the logic into a module. Then put your “testing” logic into another module. Then just use the config to determine which module you want to use.
defmodule YourModule do
def your_function(pool_name, endpoint) do
# some real logic
end
end
defmodule YourModuleMock do
def your_function(_pool_name, _endpoint) do
{:ok, :received}
end
end
I’ve tested it with releases (using Distillery) and it doesn’t work, which means you cannot use anonymous functions if you use releases at some point.
In particular elixirc_paths. The elixirc_paths/1 function returns the compilation paths based on the mix environment. Further down:
# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
So for :testtest/support is added to the compilation path. So all .ex files only used for testing go under test/support and those modules are then available for all the .exs testing scripts.
In combination with values set in config.exs and compile time use of Application.fetch_env!/2 the appropriate module can then be selected (example).