首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于分页的RSpec控制器测试

用于分页的RSpec控制器测试
EN

Stack Overflow用户
提问于 2016-01-23 18:22:54
回答 1查看 1.7K关注 0票数 0

我正在添加一些控制器测试,以确保我的分页工作正常。我使用的是gemfile "Will-paginate",它会在30个用户时自动添加分页。在这个测试中,我添加了31个用户并查找选择器,但是我得到的错误告诉我分页从未出现过。我做错了什么?

谢谢你们!

HAML:

代码语言:javascript
复制
= will_paginate @users, :class => 'pagination'

user_controller_spec.rb

代码语言:javascript
复制
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

错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-01-23 23:49:06

之前和现在删除的答案是正确的。您需要在执行get之前创建用户。你的问题和另一个答案的问题都是使用let创建用户,这是懒惰的评估。尝试使用let!定义用户,或者将用户的创建放在before中,如下所示,它还使用subject将设置与测试中的代码分开。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34962267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档