Question

I'm trying to display the list of users in the application. To do this, I use will_paginate gem. But when I try to change the number of users displayed on a page, tests fall, despite the fact that the application continues to work correctly (I think so).

Piece of code from user_pages_spec.rb

describe "pagination" do

  before(:all) { 30.times { FactoryGirl.create(:user) } }
  after(:all)  { User.delete_all }

  it { should have_selector('div.pagination') }

  it "should list each user" do
    User.paginate(page: 1).each do |user|
      expect(page).to have_selector('li', text: user.name)
    end
  end
end

Code from factories.rb

FactoryGirl.define do
  factory :user do
    sequence(:name) { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end
end

users_controller.rb

class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    #WANT TO CHANGE, DOESN'T WORK
    #@users = User.order('name ASC').paginate(:page => params[:page], :per_page => 8)

    #WORK
    #@users = User.order('name ASC').paginate :page => params[:page]

    #WORK
    #@users = User.paginate(:page => params[:page], :per_page => 30)

    #DOESN'T WORK
    @users = User.paginate(:page => params[:page], :per_page => 8)
  end
...
end

Gemfile:

...
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
...

Results of command bundle exec rspec spec/requests/user_pages_spec.rb

Failures:

  1) User pages index pagination should list each user
     Failure/Error: expect(page).to have_selector('li', text: user.name)
       expected #has_selector?("li", {:text=>"Person 38"}) to return true, got false
     # ./spec/requests/user_pages_spec.rb:26:in `block (5 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:25:in `block (4 levels) in <top (required)>'

Finished in 5.51 seconds
32 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:24 # User pages index pagination should list each user

It works only when in user_pages_spec.rb and in users_controller.rb is number 30 (default value for will_paginate). Falling just this test, other are passing. A little help would be nice.

Was it helpful?

Solution

add the same change on rspec spec/requests/user_pages_spec.rb

User.paginate(page: 1, :per_page => 8).each do |user|
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top