Skip to content
A simple way to describe dependencies that can be replaced at test time.
Elixir
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
lib
test
.formatter.exs
.gitignore
LICENSE.md
README.md
mix.exs
mix.lock

README.md

Injex

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

Installation

def deps do
  [{:injex, "~> 1.0"}]
end

Usage

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.

You can’t perform that action at this time.