Skip to content
forked from blatyo/injex

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

License

Notifications You must be signed in to change notification settings

cinch-financial/injex

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Injex

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

Installation

If available in Hex, the package can be installed as:

  1. Add injex to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:injex, "~> 0.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 hardcodes the default dependency.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Elixir 100.0%