tesla v0.9.0 Tesla.Mock View Source

Mock adapter for better testing.

Setup

# config/test.exs
config :tesla, adapter: :mock

# in case MyClient defines specific adapter with `adapter :specific`
config :tesla, MyClient, adapter: :mock

Example test

defmodule MyAppTest do
  use ExUnit.Case

  setup do
    Tesla.Mock.mock fn
      %{method: :get} ->
        %Tesla.Env{status: 200, body: "hello"}
    end

    :ok
  end

  test "list things" do
    assert %Tesla.Env{} = env = MyApp.get("...")
    assert env.status == 200
    assert env.body == "hello"
  end
end

Setting up mocks

# Match on method & url and return whole Tesla.Env
Tesla.Mock.mock fn
  %{method: :get,  url: "http://example.com/list"} ->
    %Tesla.Env{status: 200, body: "hello"}
end

# You can use any logic required
Tesla.Mock.mock fn env ->
  case env.url do
    "http://example.com/list" ->
      %Tesla.Env{status: 200, body: "ok!"}
    _ ->
      %Tesla.Env{status: 404, body: "NotFound"}
end

# mock will also accept short version of response
# in the form of {status, headers, body}
Tesla.Mock.mock fn
  %{method: :post} -> {201, %{}, %{id: 42}}
end

Link to this section Summary

Functions

Setup mocks for current test

Link to this section Functions

Link to this function mock(fun) View Source
mock((Tesla.Env.t -> Tesla.Env.t | {integer, map, any})) :: no_return

Setup mocks for current test.