0

我正在尝试测试我的 factoryGirl 工厂:

#spec/factories/post.rb
FactoryGirl.define do
 factory :post do |p|
  p.title "Title"
  p.body "Body"
 end
end

FactoryGirl.factories.map(&:name).each do |factory_name|
 describe "The #{factory_name} factory" do
  it 'is valid' do
   build(factory_name).should be_valid
  end
 end
end

按照这个

但我有这个错误:

in `block in <top (required)>': uninitialized constant Factory (NameError)
4

1 回答 1

1

也许试试这个:

在 /spec 中创建一个名为 factory_spec.rb 的单独文件,如下所示

require 'spec_helper'

FactoryGirl.factories.map(&:name).each do |factory_name|
  describe "The #{factory_name} factory" do
     it 'is valid' do
      build(factory_name).should be_valid
     end
  end
end

记得包括 require 'spec_helper' 行

于 2013-10-25T10:50:47.917 回答