Mockingbird v0.2.0 Mockingbird

Mockingbird helps you create API consumers that are easy to test.

Usage

# lib/my_app/github.ex
defmodule MyApp.Github do
  use Mockingbird, test: MyApp.MockGithubHttpClient

  def get_account_info(username) do
    http_client().call(:get, "https://api.github.com/users/" <> username)
  end
end

# test/support/mock_github_http_client.ex
defmodule MyApp.MockGithubHttpClient do
  use Mockingbird.Client

  # All the `call` methods you plan to use in tests will need a function head
  # that will match test usage
  def call(:get, "https://api.github.com/users/amencarini") do
    respond :ok, 200, """
    {
      "login": "amencarini",
      "id": 1100003
    }
    """
  end
end

# test/my_app/github_test.exs
defmodule MyApp.GithubTest do
  use ExUnit.Case

  describe "MyApp.Github.get_account_info/1" do
    test "it returns data for the selected user" do
      {:ok, res} = MyApp.Github.get_account_info("amencarini")
      assert Poison.decode(res.body) == %{"login" => "amencarini", "id" => 1100003}
    end
  end
end

Options

use Mockingbird, test: MyApp.MockGithubHttpClient, live_client: MyApp.RealHttpClient
  • test: specify what module will contain the mocked responses when used in the test environment.
  • live_client: use a custom http client for live calls. Mockingbirg comes with a client that performs calls through HTTPoison.