rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.18k stars 1.04k forks source link

be_successful on a request spec hangs #2592

Open jcoyne opened 2 years ago

jcoyne commented 2 years ago

I'm seeing behavior where using be_successful hangs (rather than fails) when the request status is not a 200.

hangs:

      get "/items/#{id}"
      expect(response).to be_successful

Fails right away:

      get "/items/#{id}"
      expect(response).to have_http_status(:ok)

with:

     Failure/Error: expect(response).to have_http_status(:ok)
       expected the response to have status code :ok (200) but it was :forbidden (403)

Using:

    rspec-rails (5.1.1)
    rspec-core (3.11.0)
    rails (7.0.2.3)
pirj commented 2 years ago

Shouldn't it be expect(response).to be_ok?

Would expect(response.successful?).to be true hang, too?

jcoyne commented 2 years ago

@pirj I think that be_successful is true for any 2xx based response. (e.g. :ok, :created, :no_content)

When I use expect(response.successful?).to be true it fails right away as expected.

darrenterhune commented 5 months ago

I'm having this same problem when going to rails >= 7.0 from latest supported version 6.1.3.3 (at time of writing). I've narrowed it down to something that changed in ActionDispatch::Request with sessions. I mock auth (I'm using auth0 and mock_auth). It would set the session[:user_id] which is visible in a request spec prior to issuing the get()... but inside our auth logic at the controller level, that key is nil. So it 403's.

pirj commented 5 months ago

Does it hang for you, too, @darrenterhune ?

darrenterhune commented 5 months ago

I wouldn't say it hangs. It errors because I have an expectation of it to be 200 but it is a 403.