Rspec分页

时间:2019-03-23 07:48:15

标签: rspec ruby-on-rails-5

在我的Rails应用程序中,will_paginate gem用于对帖子进行分页。每页显示10个帖子。因此,我正在写信以检查该分页是否显示每页10个帖子。

最初我创建了12个帖子。和 我已经检查了页面2.它显示的是所有帖子还是2个帖子。它仅显示2个帖子

it 'does paginate records' do
  12.times {FactoryBot.create(:post, topic_id: topic.id, user: controller.current_user)}
  get :index, params: {topic_id: topic.id, start_date: Time.now - 5.days, end_date: Time.now + 5.day, page: 2}
  p (assigns(:posts))
 end

我需要在第2页上显示2个帖子吗?如果有什么解决方法可以检查第2页有2个帖子?

2 个答案:

答案 0 :(得分:1)

it 'does paginate records for second page' do
   12.times {FactoryBot.create(:post, :post4, topic_id: topic.id, user: controller.current_user)}
   get :index, params: {topic_id: topic.id, start_date: Time.now - 5.days, end_date: Time.now + 5.day, page: 2}
   expect(assigns(:posts).map(&:id).count).to eq 2
   expect(Post.count).to eq(12)
 end

答案 1 :(得分:0)

it 'does paginate records' do
  first_two = create_list(:post, 2, topic_id: topic.id, user, controller.current_user }
  create_list(:post, 10, topic_id: topic.id, user: controller.current_user }

  get :index, params: {topic_id: topic.id, start_date: Time.now - 5.days, end_date: Time.now + 5.day, page: 2}
  expect(assigns(:posts)).to eq first_two
end