Search code examples
ruby-on-railsruby-on-rails-4rspec-railsransackrspec3

Passing parameters for Testing Ransack Search output with RSpec 3


The first time I use Ransack for searching. I have a controller like this

class CoursesController < ApplicationController
  def search
        @q = Post.joins(:events).where(status: true, publish: true)
                                                        .where("events.status = 'available'")
                                                        .search(params[:q])
        @courses = @q.result.includes(:tagnames).to_a.uniq

        render :index
  end
end

In the route.rb, it is included the route

get 'posts/autocomplete_tag_or_subcategory', to: 'posts#get_suggestion_keywords', as: :list_suggestion

and

 resources :courses, only: [:search] do
    collection do
      match 'search' => 'courses#search', via: [:get, :post], as: :search
    end
  end

In the view, we have the form with the field like this

= search_form_for @q, url: search_courses_path, method: :post do |f|
   = f.search_field :title_or_description_or_tagnames_title_cont, { data: {autocomplete:  list_suggestion_path } }

Everything seems work correctly. The thing is that, I dont know how to pass the parameters for the test case for this controller. I have tried:

it "get all records match the key words" do

            get :search, q: {title_or_description_or_tagnames_title_cont: 'math'}

            expect(assigns_courses).to include math_course
        end

the whole rspec file

RSpec.describe CoursesController , type: :controller do
    describe "#search" do
        let!(:search_params) { 'math' }
        let!(:tutor) { create :user, user_type: :tutor }
        let!(:math_course) { create(:post, title: "math", user_id: tutor.id) }
        let(:assigns_courses) { assigns(:courses) }

    before { @request.env['devise.mapping'] = Devise.mappings[:user] }
    before { sign_in tutor }



        it "get all records match the key words" do

            get :search, q: {title_or_description_or_tagnames_title_cont: 'math'}

            expect(assigns_courses).to include math_course
        end
    end
end

And this is Post model

class Post < ActiveRecord::Base
  scope :published, -> { where(status: true, publish: true) }
  scope :get_post_by_tag_name, -> (tag_name) { where("lower(tags) LIKE ? ", "%#{ tag_name.downcase }%") }

  belongs_to :category
  belongs_to :sub_category
  belongs_to :level
  belongs_to :user
  has_many   :events, dependent: :destroy

.....

Event Model

class Event < ActiveRecord::Base
  extend Enumerize

  enumerize :status, in: [:available, :booked, :hidden], default: :available

  belongs_to :post

end

But look like it incorrect because it did not show what I wanted to see. Please give me some help to pass params for this test case.

Many thanks.


Solution

  • The gem ransack should not have bug and your code looks correct to me.

    I recommend you run the test again as just get :search and see what is the problem in the first place (eg. maybe published is not set, etc).