egze

egze

How to stub Req in development?

In the docs it says I can stub Req in tests with a config like this:

# config/test.exs
config :myapp, weather_req_options: [
  plug: {Req.Test, MyApp.Weather}
]

What I want is - the same but for development to not make real API calls (they cost money) but to play with the logic.

I added the same config for development, and added this to application.ex

Req.Test.stub(MyApp.Weather, fn conn ->
  Req.Test.json(conn, %{"celsius" => 25.0})
end)

but when I make a call in iex, I get an error ** (RuntimeError) cannot find mock/stub MyApp.Weather in process #PID<0.451.0>

Only when I rerun the Req.Test.stub snippet in iex, then it gets stubbed.

Can I somehow configure Req to globally stub in development?

It works if also add it to .iex.exs but it will then only stub in iex session.

Marked As Solved

egze

egze

I think I came up with a solution. I implemented a plug module MyApp.WeatherStub with a call function and configured Req to use it in development.

Last Post!

D4no0

D4no0

You can also take a look at bypass: Bypass — bypass v2.1.0

Since it’s starting an actual web server, you shouldn’t have any problems with process scopes from tests.

Where Next?

Popular in Questions Top

New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics 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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New

We're in Beta

About us Mission Statement