req icon indicating copy to clipboard operation
req copied to clipboard

transport_error/2 raises when testing, instead of erroring

Open lucavenir opened this issue 4 months ago • 3 comments

I've read the documentation about transport_error/2 and maybe I'm understanding it wrong.

Following the docs as closely as I can, here's my (unexpectedly) failing test:

test "returns the error when the request fails" do
  Req.Test.stub(MyApp.MyModule, fn conn ->
    Req.Test.transport_error(conn, :some_error)
  end)

  assert {:error, :some_error} = my_get_function()
end

The test fails not because the match fails, but because my_get_function/0 raises:

(ArgumentError) unexpected Req.TransportError reason: :some_error

But I'm not using Req.get!/2. I'm using Req.get/2 just like the docs suggest. Here's my_get_function/0:

@doc false
def my_get_function() do
  :my_app
  |> Application.get_env(MyApp.MyModule, [])
  |> then(&Req.get(@url, &1)) # `get`, and not `get!`
  |> case do
    {:ok, %Req.Response{status: 200, body: body, headers: headers}} -> {:ok, body, headers}
    {:ok, response} -> {:error, response}
    error -> error
  end
end

Is there something I'm missing? Is this intended behavior?

lucavenir avatar Oct 12 '24 21:10 lucavenir