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