我正在添加一些控制器测试,以确保我的分页工作正常。我使用的是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发布于 2016-01-23 23:49:06
之前和现在删除的答案是正确的。您需要在执行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
endhttps://stackoverflow.com/questions/34962267
复制相似问题