Factory Girl ArgumentError:工厂未注册:

时间:2015-08-05 03:34:53

标签: ruby-on-rails ruby rspec factory-bot

我已经看了所有正确的步骤,我继续得到这个错误。 来自gemfile:

 group :test do
  gem 'rspec-rails'
  gem 'shoulda-matchers', require: false
  gem 'database_cleaner'
  gem 'factory_girl_rails', '~> 4.0', require: false
  gem 'faker'
 end

spec_helper.rb:

require 'factory_girl_rails'
RSpec.configure do |config|
 config.include FactoryGirl::Syntax::Methods
 FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
 FactoryGirl.find_definitions
 FactoryGirl.factories.clear
end

规格/工厂/ company.rb:     要求' spec_helper'     要求' faker'

FactoryGirl.define do
  factory :company do
    name { Faker::Name.name }
    system_name { Faker::Company.name }
    domain { Faker::Internet.url }
  end
end

company_spec.rb:     要求' spec_helper'

describe 'Company' do
  it 'has a valid factory' do
    FactoryGirl.build(:company).should be_valid
  end
end

获取错误失败/错误:

Failure/Error: FactoryGirl.create(:company).should be_valid
     ArgumentError:
       Factory not registered: company

1 个答案:

答案 0 :(得分:0)

我把所有东西都放在了错误的地方:

gemfile需要:

group :development, :test do
  gem 'rspec-rails'
  gem 'shoulda-matchers', require: false
  gem 'database_cleaner'
  gem 'factory_girl_rails', '~> 4.0', require: false
  gem 'faker'
 end

spec_helper中的所有内容我转移到rails_helper.rb并删除了两行:

require 'factory_girl_rails'
RSpec.configure do |config|
 config.include FactoryGirl::Syntax::Methods
 FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
 // FactoryGirl.find_definitions // got rid of this
 // FactoryGirl.factories.clear // got rid of this
end

company_spec.rb:

require 'rails_helper'

describe 'Company' do
 it 'has a valid factory' do
  expect(build(:company)).to be_valid
 end
end

这些信息大部分来自评论,并源于此tutorial关于rspec和工厂女孩。