Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport_error/2 raises when testing, instead of erroring #420

Open
lucavenir opened this issue Oct 12, 2024 · 3 comments
Open

transport_error/2 raises when testing, instead of erroring #420

lucavenir opened this issue Oct 12, 2024 · 3 comments

Comments

@lucavenir
Copy link

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?

@wojtekmach
Copy link
Owner

Thank you for the report, we definitely need to improve the error message.

The idea was you can only call transport_error/1 with errors that can happen in production so your test is more realistic. So transport_error(conn, :timeout) works but transport_error(conn, :timeout_typo) does not.

@lucavenir
Copy link
Author

Oh. This makes sense. And I like the choice! Thanks for the (very quick and appreciated!) response!

@lucavenir
Copy link
Author

lucavenir commented Oct 12, 2024

It would also be nice to know which atoms we're allowed to inject there.

:timeout isn't always that handy: I might want to retry in production, but I'd like to avoid that specific error case when testing (else, I must wait several seconds).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants