0

How can I match the following array with 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 need to check if a class method return an array of struct like the previous or a more extensive one which include the previous.

UPDATE:

I currently have this test which go green,

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

but I'd like to know if it is the correct way to test the following method or you think it could be better :

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 に答える