2017-01-04 85 views
0

我试图测试已验证路由的控制器。我正在使用OmniAuth和Google OAuth2.0。我在OmniAuth wiki上关注了this guide,以便进行集成测试。但是,我的测试失败,出现以下错误:使用RSpec登录到应用程序

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)>' 

暗示请求仍在重定向。

我已经把所有相关的文件中this Gist

回答

1

它似乎没有在您的测试设置session[:user_id]

认为你会做到这一点是这样的:为get动作发生params散列,和会话散列(如讨论here

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 

您还需要创建User记录,以便User.find不返回nil。

+0

这样做的窍门,谢谢! – Carpetfizz

+1

耶!祝你好运,快乐的编码... – jvillian

+0

此外,[OmniAuth文档](https://github.com/omniauth/omniauth/wiki/Integration-Testing)没有提及的任何原因?只是想知道我的设置是否与标准有所不同...... – Carpetfizz