Skip to content

Cookbook: Static response adapter

Tymon Tobolski edited this page Sep 11, 2018 · 1 revision

Sometimes it might be handy to fake 200 OK response (or any other) for all requests to one service in tests (for example metrics/analytics services).

Instead of setting up mock for every test you can use a static adapter:

# lib/myapp/adapters/static.ex
defmodule MyApp.Adapters.Static do
  def call(_env, :ok), do: {:ok, %Tesla.Env{status: 200}}
end

# config/test.exs
config :tesla, MyApp.MyStatsClient, adapter: {MyApp.Adapters.Static, :ok}