Bypass testing two api calls in one function

Overview

I have a 1 function that hits two api endpoints. I have been using bypass and it’s been helpful, but so far my testing has been working with functions that make single api calls. How do I test that this one function is making two api calls.

This make two api calls

#...omit code
defmodule Base do
  def get_schedule(code, base_url \\ Config.base_url()) do
    with    {:ok, schedule}        <- Api.get_schedule(code, base_url),
            {:ok, team}            <- Api.get_team(code, base_url) do
            {:ok, schedule, team}
     else
         {:error, msg} -> {:error, msg}
    end
  end
end

Bypass

I was reading the docs and thought this might work using Bypass.expect_once but it’s not working.


describe "get_schedule/2" do
    setup [:schedule_json]
    test "success, GET returns schedule, [Status: 200]",  %{bypass: bypass, json: json} do

        # Api.get_schedule(code, base_url)
       Bypass.expect_once(bypass, "GET", "/schedule/1", fn conn ->
          assert "GET" == conn.method
          assert "/schedule/1" == conn.request_path
          conn
          |> Plug.Conn.resp(200, json)
        end)

        #  Api.get_team(code, base_url)
        Bypass.expect_once(bypass, "GET",  "/teams/1", fn conn ->
           assert "GET" == conn.method
           assert "/teams/1" == conn.request_path
          conn
          |> Plug.Conn.resp(200, json)
        end)

      assert {:ok, schedule, team} = Base.get_schedule("BA", endpoint_url(bypass.port))
 
    end
end

Any feedback would be appreciated

Can you elaborate about what this means? What happens when you try to run the code that is here?

I’m getting an error like this

 Assertion with == failed
      code:  assert "/teams/1" == conn.request_path
      left:  "/teams/1"
      right: "/schedule/1"

and

** (Jason.DecodeError) unexpected end of input at position 0

Seems like you cannot stack expect_once calls this way. I’d consult the docs if this is possible or open up an issue in Bypass if this is not doable.