Pact

A module for managing dependecies in your applicaiton without having to “inject” dependencies all the way down your aplication. Pact allows you to set and get dependencies in your application code, and generate fakes and replace modules in your tests.

To use Pact, define a module in your application that has use Pact in it, and then call start_link on it to start registering your dependencies.

Usage

defmodule MyApp.Pact do
  use Pact

  register "http", HTTPoison
end

MyApp.Pact.start_link

defmodule MyApp.Users do
  def all do
    MyApp.Pact.get("http").get("http://foobar.com/api/users")
  end
end

Then in your tests you can use Pact to replace the module easily:

defmodule MyAppTest do
  use ExUnit.Case
  require MyApp.Pact

  test "requests the corrent endpoint" do
    fakeHTTP = MyApp.Pact.generate :http do
      def get(url) do
        send self(), {:called, url}
      end
    end

    MyApp.Pact.replace "http", fakeHTTP do
      MyApp.Users.all
    end

    assert_receive {:called, "http://foobar.com/api/users"}
  end
end

Functions / Macros

Source