我添加了一些控制器测试以确保我的分页正常工作。我使用gemfile" Will-paginate",它会自动为30个用户添加分页。在这个测试中,我添加了31个用户并查找选择器,但我收到的错误告诉我,分页永远不会出现。我做错了什么?
谢谢你们!
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 :(得分: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