RSpec& amp; amp; amp; amp; amp; amp; amp;工厂女工?

时间:2012-06-16 17:16:50

标签: ruby-on-rails ruby unit-testing rspec tdd

我必须与rspec和工厂女孩隔离测试类别导轨模型。我开始用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对象,如下所示:

  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&amp; amp;分离测试这个的最佳方法是什么?工厂女工?我想到了类似下面的东西,但我不能说清楚:

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

测试这样的方法的正确方法是什么?

1 个答案:

答案 0 :(得分: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似乎未使用。