2014-07-16 58 views
2

我想編寫一個控制器測試來檢查成功登錄時,用戶是否被重定向到某個頁面。目前的測試我此刻正在返回一個200在rspec中測試重定向並設計

require 'rails_helper' 
RSpec.describe Admin::EntriesController, :type => :controller do 

setup_factories 
    describe "after login" do 
     it "should redirect to pending after logged in" do 
      sign_in admin 
     expect(response).to redirect_to('admin/entries/pending') 
     end 
    end 

end 

返回

Failure/Error: expect(response).to redirect_to('admin/entries/pending') 
     Expected response to be a <redirect>, but was <200> 

相關的控制器

class AdminController < Devise::RegistrationsController 

    before_filter :authenticate_admin! 

    protected 

    def after_sign_in_path_for(admin) 
    pending_admin_entries_path 
    end 

end 

我會嘗試這是正確的方式,其中,我錯了嗎?

感謝

回答

2

sign_in用戶的RSpec不作要求,所以你不能測試重定向。

對於after_sign_in_path,您可以測試這樣的:

it 'redirects user to pending admin entries path' do  
    expect(controller.after_sign_in_path(user)).to eq pending_admin_entries_path 
end 
+0

謝謝,這是有道理的。 –

+1

由於'after_sign_in_path'是受保護的方法,因此這可能不起作用。查看http://stackoverflow.com/questions/6658251/how-to-test-after-sign-in-path-forresource獲取更完整的答案。 – tirdadc