[erlang-questions] Mocks and explicit contracts
Thu Nov 19 13:36:21 CET 2015
José Valim suggests a way to use dependency injection to more easily
unit test Elixir code.
However, it doesn't translate to Erlang...
In one example, he stores the dependency (as a module) in the
application environment and then defines a private function to
defp twitter_api do
Then it can be called as:
This doesn't translate to Erlang (or, rather, it's unwieldy), because
we can't do the following:
application:get_env(my_app, twitter_api, default_twitter_api).
% syntax error before: ':'
We need more brackets:
This gets a bit ugly, though less so with macros:
% note the extra brackets:
(application:get_env(my_app, twitter_api, default_twitter_api))).
He goes on to show another form of DI which takes advantage of the
fact that Elixir allows inline modules, Again, we can't do this in
- Is Jose's approach applicable to Erlang at all?
- Are there idiomatic ways to do something like this in Erlang?
I know about (and use) 'meck', which is awesome, but has the
disadvantage that it mocks (verb) globally, which limits running tests
in parallel. It can also be quite slow, because it compiles the mocks
on the fly. José's approach uses mocks (noun) locally.
More information about the erlang-questions