MoxInject
Allows straightforward injection of mocks (using Mox or Hammox) into any Elixir project, using behaviours and module attributes.
How to use it
use MoxInject do
alias Module, as: @attr
...
end
...
@attr.f()
and with minimal setup in tests, using Mox or Hammox for mocking:
Module.Mock |> expect(:f, ...)
How it works
Depending on the value of config :mox_inject, :test_dependencies?
:
- when
false
, the actual module that was aliased is injected and used in the module (e.g.X
). - when
true
, it injects a module with a.Mock
suffix e.g.X.Mock
) into the module.- during tests, this mock module is set up to use
Mox
orHammox
;expect
,stub
can be used on the module
- during tests, this mock module is set up to use
There are two types of mocks that can be set up this way:
- either the modules already have a
Behaviour
submodule already defined (modules_with_behaviour_submodules
) - or adding a behaviour module for a module that does not have an existing behaviour defined (
explicit_behaviours
)