工厂女孩:工厂未注册:用户(ArgumentError)

时间:2021-10-08 11:19:56

Having a lot trouble getting all the ducks in the right order with FactoryGirl.

我很难把所有的鸭子按正确的顺序排列好。

Set up a minimalist rails app (3.0.11), factory_girl_rails (1.4.0), factory_girl (2.3.2) & cucumber-rails (1.2.1) and ruby-1.8.7-p352.

建立一个极简的rails应用程序(3.0.11),factory_girl_rails (1.4.0), factory_girl(2.3.2)和-rails(1.2.1)和ruby-1.8.7-p352。

The cucumber test is:

黄瓜测试:

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

The result is this:

结果是这样的:

cucumber 
Using the default profile...
"Adam Advertiser"
"a@b.com"
#<User id: nil, name: nil, created_at: nil, updated_at: nil, email: nil>
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

It would seem a load sequence problem, maybe. Would appreciate any help to get this right. Regards Ross

这似乎是一个负载序列问题。如果你能帮我,我将不胜感激。认为罗斯

features/Factories.rb is thus:

特性/工厂。因此,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'

My features/support/env.rb is:

我的特性/支持/ env。rb:

require 'pp'
require 'cucumber/rails'

require 'factory_girl_rails'

My GemFile is:

我的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

4 个解决方案

#1


32  

Calling FactoryGirl.find_definitions right after the require 'factory_girl_rails' fixed a similar problem for me.

调用FactoryGirl。在require“factory_girl_rails”之后,find_definition为我解决了一个类似的问题。

See Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT

看到不能让factory_girl在rails 3.0.5下运行,意外的tCONSTANT。

#2


0  

I am new to FactoryGirl and I faced a similar error as mentioned in this post.The error I faced was

我是工厂女孩的新员工,我也遇到了类似的错误。我所面临的错误是

 ArgumentError:
   Not registered: client

Factory setup:

工厂设置:

/spec/factories.rb

/规范/ 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 - Contained child factories for :user

/规范/工厂/用户。rb -包含子工厂:用户

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

I was running the spec using following command:

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

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

Thanks to @Ross.The Gem file shown clicked me what was missing.

感谢@Ross。显示的Gem文件单击了我所缺少的内容。

Thanks.

谢谢。

#3


0  

I solved this problem simply add these lines on my spec_helper:

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

require 'factory_girl'
FactoryGirl.find_definitions

#4


0  

As per this answer, adding FactoryGirl.find_definitions will work, but adding require 'rails_helper' should eliminate the need for that if you haven't done that.

根据这个答案,添加factory - girl。find_definition将会起作用,但是添加require 'rails_helper'应该消除了如果您没有这样做的需要。

#1


32  

Calling FactoryGirl.find_definitions right after the require 'factory_girl_rails' fixed a similar problem for me.

调用FactoryGirl。在require“factory_girl_rails”之后,find_definition为我解决了一个类似的问题。

See Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT

看到不能让factory_girl在rails 3.0.5下运行,意外的tCONSTANT。

#2


0  

I am new to FactoryGirl and I faced a similar error as mentioned in this post.The error I faced was

我是工厂女孩的新员工,我也遇到了类似的错误。我所面临的错误是

 ArgumentError:
   Not registered: client

Factory setup:

工厂设置:

/spec/factories.rb

/规范/ 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 - Contained child factories for :user

/规范/工厂/用户。rb -包含子工厂:用户

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

I was running the spec using following command:

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

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

Thanks to @Ross.The Gem file shown clicked me what was missing.

感谢@Ross。显示的Gem文件单击了我所缺少的内容。

Thanks.

谢谢。

#3


0  

I solved this problem simply add these lines on my spec_helper:

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

require 'factory_girl'
FactoryGirl.find_definitions

#4


0  

As per this answer, adding FactoryGirl.find_definitions will work, but adding require 'rails_helper' should eliminate the need for that if you haven't done that.

根据这个答案,添加factory - girl。find_definition将会起作用,但是添加require 'rails_helper'应该消除了如果您没有这样做的需要。