2012-09-11 2 views
3

Tire gem을 테스트하기위한 구문을 이해하려고합니다.RSpec & Tire gem : Tire 테스트 :: 결과 :: 컬렉션

이 컨트롤러의 사양 (발판 템플릿에서 기본은) 실패

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) 

때문에 활성 레코드 개체의? 아니면이 문제를 해결할 더 좋은 방법이 있습니까?
class ReportsController < ApplicationController 
    def index 
    @reports = Report.search(params) 
    end 

    ... 

하고 모델

FWIW-

는 :

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

<귀뚜라미 .....> – Meltemi

답변

2

나는이 미친 듯이 늦게 대답 실현하지만, 이봐, 여기에 표시됩니다.

Rspec은 직접 비교를 수행하고 있습니다. 컬렉션이 있고 그것을 배열과 비교하려고합니다. 그러나 Tire는 실제로 배열을 반환하지 않는 배열로 캐스트를 정의합니다 (왜 필자는 잘 모르겠다. 나에게 짜증나!)

배열을 비교할 의도가 없으므로 나는 컬렉션 소스에서 빠르게 들여다보기 : https://github.com/karmi/tire/blob/master/lib/tire/results/collection.rb

음, 우리는 to_ary를 유용하지 않지만 우리는 각각 하나씩 Enumerable을 포함하고 있습니다. 즉, 기본적으로 배열에 사용할 수있는 모든 것이 있습니다.

그래서, 우리가 실제로 여기서 무엇을하기를 원합니까? 우리는 @report가 @reports 내에서 사용 가능한지 확인하고자합니다. 글쎄, 우리는 열거 할 수 있고 예상 소스 (https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/matchers/built_in/include.rb#L38)의 빠른 점검은 include가 포함 할지도 모른다고 말하고있다. arrayesque 오브젝트에.

그래서, 한마디로 테스트를 변경해보십시오 :

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 
+0

결코보다는 늦게 더 나은! 좋은 대답입니다! 감사! – Meltemi