Carpetfizz
Carpetfizz

Reputation: 9149

Logging into application with RSpec

I am trying to test a controller that has authenticated routes. I'm using OmniAuth with Google OAuth2.0. I followed this guide on the OmniAuth wiki to set it up for integration testing. However, my test is failing with the following error:

1) GroupsController GET #show when user logged in responds with HTTP 200 and a group object
     Failure/Error: expect(response).to have_http_status(200)
       expected the response to have status code 200 but it was 302
     # ./spec/controllers/groups_controller_spec.rb:27:in `block (4 levels) in <top (required)>'

suggesting that the request is still being redirected.

I've put all the relevant files in this Gist

Upvotes: 0

Views: 126

Answers (1)

jvillian
jvillian

Reputation: 20263

It doesn't seem you set session[:user_id] in your test.

I think you would do that by something like:

  RSpec.describe GroupsController, type: :controller do
    describe 'GET #show' do
      context 'when user logged in' do
        it "responds with HTTP 200 and a group object" do
          get :show, {id: 1}, {user_id: 1}
          expect(response).to have_http_status(200)
        end
      end
    end
  end

as the get action takes a params hash and a session hash (as discussed here).

You'll also need to create the User record so that User.find doesn't return nil.

Upvotes: 1

Related Questions