2

我必须与 rspec 和 factory girl 隔离测试 Category rails 模型。我开始使用 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

现在我应该定义 & 测试,一个Category 类方法,它返回一个竞争者数组,它是一个Struct对象,如下所示:

  Object.const_set :Competitor, Struct.new(:html_url, :description, :watchers, :forks)

那么这个类方法seff.find_competitors_by_tags应该返回一个结构数组(:竞争对手结构对象数组):

  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 回答