2016-01-23 192 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