2016-01-23 121 views
0

我正在添加一些控制器測試以確保我的分頁工作正常。我使用gemfile「Will-paginate」,它會自動爲30位用戶添加分頁。在這個測試中,我添加31個用戶並查找選擇器,但是我收到的錯誤告訴我,分頁從不出現。我究竟做錯了什麼?RSpec控制器測試分頁

謝謝你們!

HAML:

= will_paginate @users, :class => 'pagination' 

user_controller_spec.rb

let(:user) { FactoryGirl.create(:user) } 

describe 'GET #index' do 
    before { get :index } 

    it { should respond_with(200) } 
    it { should render_template('index') } 
    it { should render_with_layout('application') } 
    it { should use_before_action(:authorize_user!) } 

    it 'shows pagination' do 
     users = FactoryGirl.create_list(:user, 31) 
     expect(:index).to have_css('div.pagination') 
    end 
    end 

錯誤:

1) Admin::UsersController GET #index shows pagination 
Failure/Error: expect(:index).to have_css('div.pagination') 
    expected to find css "div.pagination" but there were no matches 
+0

驗證,如果你有實際31個用戶'希望(User.count)。爲了EQ 31',如果你這樣做是正確的鏈接將顯示 ' – DevMarwen

+0

嗨Marwen, 有用的評論。謝謝!它證實有35個用戶(我以前也創造了幾個) 故障/錯誤:期待(User.count)。爲了EQ 31 預期:31 了:35 所以有35個用戶,並應顯示分頁? – Andy

+0

問題是測試仍然失敗 – Andy

回答

0

以前的和現在的答案被刪除了它的權利。您需要先創建用戶,然後再執行get。您的問題和其他答案的問題是使用let來創建用戶,該用戶會進行懶惰評估。試用let!來定義用戶,或者通過在before中創建用戶,如下所示,它也使用subject來保持設置與被測代碼分離。

describe 'GET #index' do 
    before { FactoryGirl.create(:user, 31) } 

    subject { get :index } 

    it { should respond_with(200) } 
    it { should render_template('index') } 
    it { should render_with_layout('application') } 
    it { should use_before_action(:authorize_user!) } 

    it 'shows pagination' do 
     expect(:index).to have_css('div.pagination') 
    end 
    end 
end