0

如何将以下数组与 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>]

我需要检查一个类方法是否返回一个结构数组,如前一个或更广泛的一个,其中包括前一个。

更新:

我目前有这个绿色的测试,

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
4

1 回答 1

4

我将测试确保您的查找功能正常工作所需的最低要求。您可能不需要为此检查返回记录的每个字段。你所拥有的就是这样做的。我会稍微修改一下,只看描述(或任何其他合适的字段):

  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
于 2012-06-27T18:35:41.610 回答