Dwi Prihandi
Dwi Prihandi

Reputation: 67

Railstutorial Chapter 9 User index test raised a failure after modify pagination

I'm just curious how its happen

After try to reduce number of user per page in pagination to 10 (default is 30 that I think is too long). My index test raised a failure just like this

1) Failure:
UsersIndexTest#test_index_as_admin_including_pagination_and_delete_links
[/home/ubuntu/workspace/contoh/test/integration/users_index_test.rb:17]:
Expected at least 1 element matching "a[href="/users/338193910"]", found 0..
Expected 0 to be >= 1.

What i've changed is just the pagination parameter Listing 9.42

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

in app/controllers/users_controller.rb

and here is the line 17-20 in /users_index_test.rb

assert_select 'a[href=?]', user_path(user), text: user.name
unless user == @admin
assert_select 'a[href=?]', user_path(user), text: 'delete',
                                                method: :delete

Upvotes: 2

Views: 311

Answers (2)

D4v1dW3bb
D4v1dW3bb

Reputation: 68

I had the same problem because I changed this in my app/controllers/users_controller.rb:

def index
  @users = User.paginate(page: params[:page])
end

to

def index
  @users = User.paginate(page: params[:page], per_page: 10)
end

Now i got the same error with the users_index_test

After some browsing and reading this article I came up with the following solution:

In the test/integration/users_index_test.rb I changed:

first_page_of_users = User.paginate(page: 1)

to

first_page_of_users = User.paginate(page: 1, per_page: 10)

The test is now successfull :)

Upvotes: 3

Dwi Prihandi
Dwi Prihandi

Reputation: 67

After trying some configuration

I've found that put the per_page parameter in global parameter is the solution

So, i put it in app/controller/application_controller.rb this line

WillPaginate.per_page = 10

Upvotes: 1

Related Questions