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

Req.Test influences the way integration tests are written. #416

Closed
c4710n opened this issue Sep 27, 2024 · 1 comment
Closed

Req.Test influences the way integration tests are written. #416

c4710n opened this issue Sep 27, 2024 · 1 comment

Comments

@c4710n
Copy link

c4710n commented Sep 27, 2024

In short

Req.Test is good for unit tests without requesting external services.

But, when writing tests which have to request external service, it seems that there's no way to do that.


In detail

In order to describe this issue in more detail, I'd like to use the example provided by the doc of Req.Test.

First, we use following line to make sure the request against a plug stub named MyApp.Weather:

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

Then, we write a test without requesting external service like this:

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

  assert MyApp.Weather.get_rating("Krakow, Poland") == {:ok, :nice}
end

So far so good.

But if I want to add an integration test which ensures "the structure of response of the external service doesn't change":

test "the structure of the response of external service doesn't change" do
   assert {:ok,
            %{
              status: 200, 
              body: %{"celsius" => _}
            }
          } = MyApp.Weather.get_temperature("Krakow, Poland")

   # it will report:
   # (RuntimeError) cannot find mock/stub MyApp.Weather in process #PID<0.519.0>
end

Above test won't work, because we have set a global :plug option in test environment, and there's no way to opt out.

Is there any way to write such type of tests?

@wojtekmach
Copy link
Owner

There's no easy way to do that at the moment, more details in #385. I'm going to close but feel free to continue discussing this in either issue.

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