neuone

neuone

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

First Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement