florblue

florblue

Mock http request using Tesla.Mock

Hi everyone!

I would like to test some endpoints that make a request API call (particularly to google oauth API and to its /tokeninfo endpoint) and I wonder how can I mock that intern call to avoid execution during test and if is a good idea using Tesla.Mock. Im not sure how to intercept the real call to mock it.

Thank you folks!

Most Liked

stefanchrobot

stefanchrobot

A better approach is to use Mox. In config/test.exs:

config :tesla, adapter: Tesla.MockAdapter

Then define the mock in test_helper.exs:

Mox.defmock(Tesla.MockAdapter, for: Tesla.Adapter)

And then mock it in test:

defmodule ​MyTest do
  use ExUnit.Case, async: true

  test "some test" do
    Mox.expect(Tesla.MockAdapter, :call, fn env, _opts ->
      # return {:ok, modified_env}
    end)

    # some code that makes an HTTP request
  end
end
girlsleuth

girlsleuth

I can’t speak toward Tesla.Mock, but I can second Mox and the “Roll Your Own” strategy Sophie DeBenedetto describes here.

.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement