FactoryGirl:工厂未注册:user(ArgumentError)

使用FactoryGirl以正确的顺序获取所有鸭子很麻烦。

设置一个简约的rails应用程序(3.0.11),factory_girl_rails(1.4.0),factory_girl(2.3.2)和cucumber-rails(1.2.1)以及ruby-1.8.7-p352。

黄瓜测试是:

Feature: a Scenario: test factory-girl Given the following user exists: | name | email | | Brandon | brandon@example.com | 

结果如下:

 cucumber Using the default profile... "Adam Advertiser" "a@b.com" # Feature: a Scenario: test factory-girl # features/users.feature:2 Given the following user exists: # factory_girl-2.3.2/lib/factory_girl/step_definitions.rb:100 | name | email | | Brandon | brandon@example.com | **Factory not registered: user (ArgumentError)** features/users.feature:3:in `Given the following user exists:' Failing Scenarios: cucumber features/users.feature:2 # Scenario: test factory-girl 1 scenario (1 failed) 1 step (1 failed) 0m0.107s 

这似乎是一个加载序列问题。 希望得到任何帮助以实现这一目标。 关心罗斯

function/ Factories.rb因此:

 FactoryGirl.define do factory :user do name 'Adam Advertiser' email 'a@b.com' end end pp FactoryGirl.create(:user) require 'factory_girl/step_definitions' 

我的function/支持/ env.rb是:

 require 'pp' require 'cucumber/rails' require 'factory_girl_rails' 

我的GemFile是:

 source 'http://rubygems.org' gem 'rails', '3.0.11' gem 'sqlite3' group :development, :test do gem 'cucumber-rails' gem 'factory_girl_rails' # rails 3 version end 

require 'factory_girl_rails' FactoryGirl.find_definitions require 'factory_girl_rails'之后立即调用FactoryGirl.find_definitions为我修复了类似的问题。

请参阅无法在rails 3.0.5下运行factory_girl,意外的tCONSTANT

我是FactoryGirl的新手,我遇到了这篇文章中提到的类似错误。我遇到的错误是

  ArgumentError: Not registered: client 

工厂设置:

/spec/factories.rb

 Factory.define :user do |f| f.email { Faker::Internet.email } f.first_name { Faker::Name.first_name } f.last_name { Faker::Name.last_name } f.phone { Faker::PhoneNumber.phone_number } f.password "foobar" end 

/spec/factories/users.rb – 包含的子工厂:user

 Factory.define :client, :parent => :user do |client| client.email { Factory.next(:client_email) } client.password "password" client.password_confirmation "password" end 

我使用以下命令运行规范:

  app_root$ RAILS_ENV=custom_test bundle exec rspec spec/controllers/users_controller_spec.rb 

感谢@ Ross。显示的Gem文件点击了我遗漏的内容。

谢谢。

我解决了这个问题,只需在我的spec_helper上添加这些行:

 require 'factory_girl' FactoryGirl.find_definitions 

根据这个答案 ,添加FactoryGirl.find_definitions会起作用,但是如果你没有这样做,添加require 'rails_helper'就不需要了。