2

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)>'

このような方法をテストする正しい方法は何ですか?

4

1 に答える 1

2

これを単独でテストする方法を尋ねました。

 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

主な問題はExtractor.eachの呼び出しです...このメソッドに配列を送信し、この依存関係を反転する必要があります。あなたのメソッドがそれ自身で情報を得るために出かけるときはいつでも、それはテストするのを難しくします。だからこのようなもの:

def self.find_competitors_by_tags(extractions, tags_array)
  extractions.map do |wl|
    Competitor.new(wl.html_url, wl.description, wl.watchers, wl.forks)
  end
end

次に、これを簡単に送信してテストできます。

let(:extractions) {[ stub(:competitor), {html_url: "url", description: "description", watchers: "watchers", forks: "forks"] }

it "should map the extractions" do
  competitors = Class.find_competitors_by_tags(extractions, nil)
  competitors.first.html_url.should eq("url")
  #continue
end

最後の注意:tags_array未使用のようです。

于 2012-06-16T21:09:28.547 に答える