【发布时间】:2015-07-22 15:07:38
【问题描述】:
我第一次使用 Ransack 进行搜索。 我有一个这样的控制器
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
在route.rb中,包含路由
get 'posts/autocomplete_tag_or_subcategory', to: 'posts#get_suggestion_keywords', as: :list_suggestion
和
resources :courses, only: [:search] do
collection do
match 'search' => 'courses#search', via: [:get, :post], as: :search
end
end
在视图中,我们有这样的字段的表单
= 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 } }
似乎一切正常。 问题是,我不知道如何为这个控制器的测试用例传递参数。 我试过了:
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
整个 rspec 文件
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
这是 Post 模型
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
.....
事件模型
class Event < ActiveRecord::Base
extend Enumerize
enumerize :status, in: [:available, :booked, :hidden], default: :available
belongs_to :post
end
但看起来它不正确,因为它没有显示我想看到的内容。 请帮我传递这个测试用例的参数。
非常感谢。
【问题讨论】:
-
错误是什么?没有assigns_courses?
-
预期结果错误,没有错误
-
是的,没有assigns_courses
标签: ruby-on-rails ruby-on-rails-4 rspec-rails ransack rspec3