Skip to content

How do I mock the same exact request, but in two different moments, with two different responses? #563

@edborsa

Description

@edborsa

Hello, fellow developers.

I was wondering if you can help me with a specific use-case of Tesla.Mock.
(Not sure if this is the best place to post this question, if not, I can repost in the proper place, but here is:)

Context:
I want to create a record in an external API, for the same url, with the same query params, for this, I create a mock like this:

%{
          method: :get,
          url: "some_url",
          query: [
            {"Foo", "Bar"},
          ]
        } ->
          %Tesla.Env{
            status: 200,
            body:
              "some_response_1.json"
              |> File.read!()
              |> Jason.decode!()
          }

This "some_response_1.json", returns something like: Oh, you can create your record because there is nothing in here.
Then I do another request to create something, but now IN THE FUTURE, I want to do the exact same request as presented above, but this time reading another json response, A.K.A. "some_response_2.json":

%{
          method: :get,
          url: "some_url",
          query: [
            {"Foo", "Bar"},
          ]
        } ->
          %Tesla.Env{
            status: 200,
            body:
              "some_response_2.json" # <-------- OTHER RESPONSE
              |> File.read!()
              |> Jason.decode!()
          }

How can I do this? if theoretically, I am pattern matching on the same Tesla props? But in different moments?

Thank you for your attention.
Best regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions