尝试理解围绕轮胎 gem 进行测试的语法。
此控制器规范(默认来自脚手架模板)失败
describe "GET index" do
it "assigns all reports as @reports" do
report = Report.create! valid_attributes
get :index, {}, valid_session
assigns(:reports).should eq([report])
end
end
因为
Failure/Error: assigns(:reports).should eq([report])
TypeError:
can't convert Tire::Results::Collection to Array (Tire::Results::Collection#to_ary gives Tire::Results::Collection)
如何编写规范以使其期望轮胎结果集合而不是活动记录对象数组?或者,有没有更好的方法来解决这个问题?
FWIW-
class ReportsController < ApplicationController
def index
@reports = Report.search(params)
end
...
和模型:
class Report < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
...
def self.search(params)
tire.search(load: true) do
query { string params[:query] } if params[:query].present?
end
end
...