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.

1 Like

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.

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.