LauraBeatris

LauraBeatris

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

Most Liked

dimitarvp

dimitarvp

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.

w0rd-driven

w0rd-driven

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.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement