Question: Logging into application with RSpec


Logging into application with RSpec

Answers 1
Added at 2017-01-04 21:01

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

nr: #1 dodano: 2017-01-04 21:01

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)

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.

Source Show
◀ Wstecz