2012-06-27 4 views
0

다음 배열을 Rspec과 어떻게 일치시킬 수 있습니까?Rspec을 사용하여 복잡한 데이터 구조 (구조체 배열) 일치

[#<struct Competitor html_url="https://github.com/assaf/vanity", description="Experiment Driven Development for Ruby", watchers=845, forks=146>, 
#<struct Competitor html_url="https://github.com/andrew/split", description="Rack Based AB testing framework", watchers=359, forks=43>] 

는 클래스 I 방식은 이전 또는 이전 포함되는 다양한처럼 구조체의 배열을 반환 여부를 확인해야한다.

UPDATE :

나는 현재 내가 당신이 그것을 생각하거나 다음 방법을 테스트하는 올바른 방법 있는지 알고 싶습니다

require 'spec_helper' 
    describe "Category" do 
    before :each do 
     @category = Category.find_by(name: "A/B Testing") 
    end 

    describe ".find_competitors_by_tags" do 
     it "returns a list of competitors for category" do 
     competitors = Category.find_competitors_by_tags(@category.tags_array).to_s 
     competitors.should match /"Experiment Driven Development for Ruby"/ 
     end 
    end 
    end 
end 

하지만 친환경이 테스트를 가지고 더 좋을 수 :

class Category 
    ... 

    Object.const_set :Competitor, Struct.new(:html_url, :description, :watchers, :forks) 
    def self.find_competitors_by_tags(tags_array) 
    competitors = [] 

    User.all_in('watchlists.tags_array' => tags_array.map{|tag|/^#{tag}/i}).only(:watchlists).each do |u| 
     u.watchlists.all_in(:tags_array => tags_array.map{|tag|/^#{tag}/i}).desc(:watchers).each do |wl| 
     competitors << Competitor.new(wl.html_url, wl.description, wl.watchers, wl.forks) 
     end 
    end 
    return competitors 
    end 
end 
+0

자세히 설명해 주시겠습니까? –

+0

@ Jason Waldrip ... 업데이트 –

답변

3

나는 당신의 찾기 기능이 제대로 작동하는지 확인하기 위해 필요한 최소한의 테스트 것입니다. 아마도 반환 된 레코드의 모든 필드를 검사 할 필요는 없습니다. 당신이 가진 것은 그렇게합니다. 설명을 보거나 (또는 ​​다른 분야가 무엇이든간에) 조금만 수정하면됩니다.

it "returns a list of competitors for category" do 
    competitors = Category.find_competitors_by_tags(@category.tags_array) 
    descriptions = competitors.map(&:description).sort 
    descriptions.should == [ 
     "Experiment Driven Development for Ruby", 
     "Rack Based AB testing framework", 
    ] 
    end 
+0

이 좋아 보이지만 실제로는 "정확한 일치 기대"로 인해 실패합니다. http://pastie.org/4161632 –

+0

다음 작품은 http://pastie.org/4161684 –

+0

으로 작동합니다. @ 루카, 왜 당신은 결과가 정확하게 당신이 기대하는 기록을 포함했는지 테스트하고 싶지 않겠습니까? –