Rajkumar P
Rajkumar P

Reputation: 179

Rspec for pagination

In my rails app will_paginate gem is used for paginating posts. It shows 10 posts per page. So, I am writing to check the pagination whether it shows the 10 posts per page or not.

Initially I have created 12 posts. and I have checked the page 2. whether it shows all posts or 2 posts. It shows only 2 posts

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

I need to show 2 posts in the page 2? If there is any solution to check the page 2 has 2 posts?

Upvotes: 3

Views: 1328

Answers (1)

Antarr Byrd
Antarr Byrd

Reputation: 26131

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

Upvotes: 3

Related Questions