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