I wanted to share
rewire with you, a dependency injection library I’ve been working on over the last few weeks. It aims to keep your production code free from testing concerns, works with async tests and any mock.
Given a module such as this:
# this module has a hard-wired dependency on the `English` module defmodule Conversation do def start(), do: English.greet() end
If you define a
EnglishMock you can rewire the dependency in your unit test:
defmodule MyTest do use ExUnit.Case use Rewire # (1) activate `rewire` import Mox rewire Conversation, English: EnglishMock # (2) rewire `English` to `EnglishMock` test "start/0" do stub(EnglishMock, :greet, fn -> "g'day" end) assert Conversation.start() == "g'day" # (3) test using the mock end end
Check out all the details at:
And let me know what you think!