简体   繁体   中英

RSpec & Tire gem: test for Tire::Results::Collection

Trying understand syntax for testing around the Tire gem.

This controller spec (default from a scaffold template) is failing

  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

because

 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)

How to write the spec so that it expects a Tire result collection instead of an array of active record objects? Or, is there a better way to go about this?

FWIW-

class ReportsController < ApplicationController  
  def index
    @reports = Report.search(params)
  end

  ...

and the model:

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
  ...

I realise this is an insanely late answer, but hey, here goes.

Rspec is doing a direct comparison. It has a collection, and it's trying to compare it to the array. However, Tyre defines the cast to an array to not actually return an array (why, I'm not sure, sounds annoying to me!)

Given as you aren't meant to compare an array, then I had a quick peek at the source of Collection : https://github.com/karmi/tire/blob/master/lib/tire/results/collection.rb

Well, we don't have a to_ary that's useful...but we do have an each, and the inclusion of Enumerable. This means that we have basically everything that's available to an array.

So, given that, what do we actually want to do here? We want to check that @report is available inside @reports. Well, we have enumerable, and a quick check of the expectations source( https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/matchers/built_in/include.rb#L38 ) says that include will map to include? on an arrayesque object.

So, in short, try changing your test to:

describe "GET index" do
  it "assigns all reports as @reports" do
   report = Report.create! valid_attributes
   get :index, {}, valid_session
   assigns(:reports).should include(report)
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM