rspecとfactorygirlとは別に、Categoryrailsモデルをテストする必要があります。次のように、FactoryGirlでカテゴリ(レコード)を定義し始めます。
#spec/factories/category.rb
FactoryGirl.define do
factory :category do |f|
f.name "A/B Testing"
f.tags_array %w(test a/b)
end
end
およびカテゴリモデルの仕様:
# spec/models/category_spec.rb
require 'spec_helper'
describe "Category" do
before(:each) do
@category = FactoryGirl.create(:category)
end
it "has a valid factory" do
@category.should be_valid
end
it "is invalid without a name" do
@category.name = nil
@category.should_not be_valid
end
it "is invalid without at last one tag" do
@category.tags_array = nil
@category.should_not be_valid
end
end
次に、次のように、StructオブジェクトであるCompetitorの競合他社の配列を返すCategoryクラスメソッドを定義してテストする必要があります。
Object.const_set :Competitor, Struct.new(:html_url, :description, :watchers, :forks)
次に、このクラスメソッドseff.find_competitors_by_tagsは、構造体の配列(:Competitor Structオブジェクトの配列)を返す必要があります。
def self.find_competitors_by_tags(tags_array)
competitors = []
Extractor.each do |wl|
competitors << Competitor.new(wl.html_url, wl.description, wl.watchers, wl.forks)
end
return competitors
end
RSpecとFactoryGirlを使用してこれを個別にテストするための最良の方法は何ですか?私は次のようなことを考えましたが、はっきりとは言えません。
spec/factories/competitor.rb
FactoryGirl.define do
factory :competitor do |f|
f.html_url "https://github.com/assaf/vanity"
f.description "Experiment Driven Development for Ruby"
f.watchers "844"
f.forks "146"
end
end
# spec/models/category_spec.rb
require 'spec_helper'
describe "Category" do
...
...
it "returns a list of all competitors for each category" do
competitors << FactoryGirl.create(:competitor)
competitors << Factory.build(:competitor, html_url: "https://github.com/andrew/split",
description: "Rack Based AB testing framework",
watchers: "357",
forks: "42")
@category.find_competitors_by_tags("A/B Testing").should == competitors
end
end
とにかく機能せず、意味があるかどうかもわかりません。
Failures:
1) Category returns a list of all competitors for each category
Failure/Error: @competitors << FactoryGirl.create(:competitor)
NameError:
uninitialized constant Competitor
# ./spec/models/category_spec.rb:22:in `block (2 levels) in <top (required)>'
このような方法をテストする正しい方法は何ですか?