Skip to content

Latest commit

 

History

History
91 lines (66 loc) · 2.26 KB

1.testing.md

File metadata and controls

91 lines (66 loc) · 2.26 KB

Testing

There are two primary ways to mock requests in Tesla:

  • Using Mox
  • Using Tesla.Mock

You can also create a custom mock adapter if needed. For more information about adapters, refer to the Adapter Guide to create your own.

Should I Use Mox or Tesla.Mock?

We recommend using Mox for mocking requests in tests because it is well-established in the Elixir community and provides robust features for concurrent testing. While Tesla.Mock offers useful capabilities, it may be removed in future releases. Consider using Mox to ensure long-term compatibility. For additional context, see GitHub Issue #241.

Mocking with Mox

To mock requests using Mox, first define a mock adapter:

# test/support/mock.ex
Mox.defmock(MyApp.MockAdapter, for: Tesla.Adapter)

Configure the mock adapter in your test environment:

# config/test.exs
config :tesla, adapter: MyApp.MockAdapter

Set up expectations in your tests:

defmodule MyApp.FeatureTest do
  use ExUnit.Case, async: true

  test "example test" do
    Mox.expect(MyApp.MockAdapter, :call, fn
      %{url: "https://github.com"} = env, _opts ->
        {:ok, %Tesla.Env{env | status: 200, body: "ok"}}

      %{url: "https://example.com"} = env, _opts ->
        {:ok, %Tesla.Env{env | status: 500, body: "oops"}}
    end)

    assert {:ok, env} = Tesla.get("https://github.com")
    assert env.status == 200
    assert env.body == "ok"
  end
end

Mocking with Tesla.Mock

Alternatively, you can use Tesla.Mock to mock requests.

Set the mock adapter in the test environment:

# config/test.exs
config :tesla, adapter: Tesla.Mock

Define mock responses in your tests:

defmodule MyAppTest do
  use ExUnit.Case, async: true

  test "list things" do
    Tesla.Mock.mock(fn
      %{method: :get, url: "https://example.com/hello"} ->
        %Tesla.Env{status: 200, body: "hello"}

      %{method: :post, url: "https://example.com/world"} ->
        Tesla.Mock.json(%{"my" => "data"})
    end)

    assert {:ok, env} = Tesla.get("https://example.com/hello")
    assert env.status == 200
    assert env.body == "hello"
  end
end

For more details, refer to the Tesla.Mock module documentation.