MoxInject - Dependency injection for Mox and Elixir

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 or Hammox; expect, stub can be used on the module

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)
1 Like

Could you show an actual example? Both here and on GitHub your writing is a bit confusing.

1 Like

Thank you for the feedback. I’ve added a test example and hopefully clarified some of the description above. Does that make it clearer to understand the library’s purpose and usage?

1 Like

It is better now, yeah.

As a general feedback: too much of the triple dots – ... – thing. :smiley:

IMO make a complete but small example. Don’t use the triple dots.

1 Like