ruby on rails - Passing parameters for Testing Ransack Search output with RSpec 3 -
the first time use ransack searching. have controller 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 route.rb, included route
get 'posts/autocomplete_tag_or_subcategory', to: 'posts#get_suggestion_keywords', as: :list_suggestion
and
resources :courses, only: [:search] collection match 'search' => 'courses#search', via: [:get, :post], as: :search end end
in view, have form field this
= search_form_for @q, url: search_courses_path, method: :post |f| = f.search_field :title_or_description_or_tagnames_title_cont, { data: {autocomplete: list_suggestion_path } }
everything seems work correctly. thing that, dont know how pass parameters test case controller. have tried:
it "get records match key words" :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 describe "#search" 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 } "get records match key words" :search, q: {title_or_description_or_tagnames_title_cont: 'math'} expect(assigns_courses).to include math_course end end end
and post model
class post < activerecord::base scope :published, -> { where(status: true, publish: true) } scope :get_post_by_tag_name, -> (tag_name) { where("lower(tags) ? ", "%#{ 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 incorrect because did not show wanted see. please give me pass params test case.
many thanks.
the gem ransack should not have bug , code looks correct me.
i recommend run test again get :search
, see problem in first place (eg. maybe published not set, etc).
Comments
Post a Comment