Fixtures for HTTP response assertions

Is there a recommended way for creating/organizing fixtures for HTTP response in order to be referenced when mocking?

Atm, on our SDK, we’re setting a fake body that doesn’t match the response payload from the API:

  describe "#delete_organization/1 with an invalid id" do
    setup do
      mock(fn
        %{method: :delete, url: "https://api.workos.com/organizations/invalid"} ->
          %Tesla.Env{status: 404, body: "Not Found"}
      end)

      :ok
    end

Ideally, we would like to pass a fixture to it for the entire response body, like this example from our Node SDK: workos-node/organizations.spec.ts at main · workos/workos-node · GitHub

1 Like

Let’s just list some libraries first:

  • exvcr
  • mock
  • patch
  • mox (only if you mock your own code, not that of a library dependency)
  • bypass

I used mock just yesterday but was made aware of patch in the meantime and will use it next time because it does seem to have more functionality.

Related thread from just yesterday: Approach for numerous edge cases, HTTP GET, exvcr - #4 by dimitarvp


Responding to your actual question, it seems that exvcr has what you need. You record “cassettes” and then specify their location in the test case file.

5 Likes

exvcr seems to follow snapshot testing, which is a more framework agnostic term. This article is a little dated Stephen Bussey - Introducing Elixir Response Snapshot Testing but the repo at GitHub - sb8244/elixir_response_snapshot: Implement snapshot testing in Elixir. has more recent changes and could be another option.

It looks like patch has a companion repo testing various mock libraries at GitHub - ihumanable/mockompare: Companion Mock Comparison Suite for Patch.

3 Likes