'Issue with using super after a query in Rails 4.2.11 Test Environment

I am currently using ruby 2.2.0 and rails 4.2.11 and writing controller tests for an application.

Specifically I am running into an issue when testing the following index action for users:

test 'should pull index with search params' do @current_user = users(:one) cookies[:auth_token] = @current_user.auth_token ##signing in user so they can run index action get 'index', search: { "term" => 'name' ##Search term index action will look for in first and last name fields } assert_response :success end

This should just query the users for any that have the string "name" in their first or last names. When I test the index action without a search parameter, it runs fine, when I do pass the parameter, I get the following error.

UsersControllertest#test_should_pull_index_with_search_params:
Actionview::Template::Error: undefined method `total_pages' for #<ActiveRecord::Relation []>
   config/initializers/will_paginate.rb:5:in `will_paginate'
   app/helpers/application_helper.rb:84:in `will_paginate'
   app/views/users/index.html.haml:30:in `_app_views_users_index_html_haml'
   test/controllers/users_controller_test.rb:15:in `block in <class:UsersControllerTest>'

Here is the index action in my users controller

 def index
    @users = if params[:search].blank?
      User.page(params[:page]).order("super_admin DESC").order(:first_name)
    else
      User.where('first_name LIKE ? or last_name LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%").order("super_admin DESC").order(:first_name)
    end      
  end

And here is the will_paginate method:

 def will_paginate(collection = nil, options = {})
      options[:renderer] ||= BootstrapLinkRenderer
      super.try :html_safe
    end

I have found that if I take out super.try :html_safe then the test runs fine. I am assuming that it is an issue with calling super in the test environment for ActiveRecord::Relation, because the action works without the query in the test environment, and this error doesn't pop up in any other environment.

Does anyone know of a work around to this, or is this an inherent limitation to Testing in Rails 4.2.11 and Ruby 2.2.0?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source