Injex: A simple way to describe dependencies that can be replaced at test time

I’ve been maintaining this library for a while and find it useful on my projects. Figured I’d share with others.

In modules, instead of:

defmodule MyModule do
  def process(data) do
    changeset = # ...

    Repo.insert(changeset)
  end
end

You write:

defmodule MyModule do
  import Injex
  inject :repo, Repo

  def process(data) do
    changeset = # ...

    repo().insert(changeset)
  end
end

Then, in your tests, you can replace Repo with a different one to simplify testing.

defmodule MyModuleTest do
  use ExUnit.Case
  import Injex.Test

  defmodule Repo do
    def insert(changeset), do: send(self, {:insert, changeset})
  end

  describe ".process" do
    test "inserts data" do
      override MyModule, repo: MyModuleTest.Repo do
        MyModule.process(%{})

        assert_received {:insert, changeset}
      end
    end
  end
end

The inject macro will only include overriding capabilities when Mix.env is :test. Otherwise, it inlines the default dependency.

4 Likes