FactoryGirl:如何在不传递参数的情况下定义工厂

发布于 2024-09-29 18:12:18 字数 5202 浏览 6 评论 0原文

我正在使用 Rails 3/factory_girl_rails/Rspec 2 和 Ruby 1.8

我已经这样定义了我的工厂:

Factory.define :user do |u|
  u.name       'Some guy'
  u.sequence(:email) {|n| "person#{n}@example.com" }
  u.password   'password'
end

Factory.define :password_reset_user, :parent => :user do |user|
  user.password_reset_key   '111222333444'
end

并且我的测试运行良好。

我注意到在工厂女孩页面上,他们在示例中定义工厂,而没有在 do 之后传递参数(http://github.com/thoughtbot/factory_girl):

# This will guess the User class
FactoryGirl.define :user do
  factory :user do
    first_name 'John'
    last_name  'Doe'
    admin false
  end

  # This will use the User class (Admin would have been guessed)
  factory :admin, :class => User do
    first_name 'Admin'
    last_name  'User'
    admin true
  end

  # The same, but using a string instead of class constant
  factory :admin, :class => 'user' do
    first_name 'Admin'
    last_name  'User'
    admin true
  end
end

为了清理我的代码,我尝试模拟定义工厂,而不在 do 之后传递参数,并将代码更改为:

Factory.define :user do
  factory :user do
    name       'Some guy'
    sequence(:email) {|n| "person#{n}@example.com" }
    password   'password'
  end

  factory :password_reset_user, :parent => :post do
    password_reset_key   '111222333444'    
  end
end

但我收到错误:

undefined method `factory' for main:Object (NoMethodError)
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:56:in `define'
    from /web/votropolis/spec/factories.rb:1
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:227:in `load_dependency'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:307:in `find_definitions'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:305:in `each'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:305:in `find_definitions'
    from /Library/Ruby/Gems/1.8/gems/factory_girl_rails-1.0/lib/factory_girl_rails/railtie.rb:11
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:34:in `call'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:43:in `run_load_hooks'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application/finisher.rb:46
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `instance_exec'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `run'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:50:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `each'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:134:in `initialize!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:77:in `send'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:77:in `method_missing'
    from /web/votropolis/config/environment.rb:5
    from /web/votropolis/spec/spec_helper.rb:3:in `require'
    from /web/votropolis/spec/spec_helper.rb:3
    from /web/votropolis/spec/controllers/organizations/memberships_controller_spec.rb:1:in `require'
    from /web/votropolis/spec/controllers/organizations/memberships_controller_spec.rb:1
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `map'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:18:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:46:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:10:in `autorun'
    from /Library/Ruby/Gems/1.8/bin/rspec:19

我做错了什么?或者这是一个兼容性问题——即,也许factory_girl_rails(适用于rails 3)与factory_girl的工作方式不同。

谢谢。

肖恩

I'm using Rails 3 / factory_girl_rails / Rspec 2 and Ruby 1.8

I've defined my factories this way:

Factory.define :user do |u|
  u.name       'Some guy'
  u.sequence(:email) {|n| "person#{n}@example.com" }
  u.password   'password'
end

Factory.define :password_reset_user, :parent => :user do |user|
  user.password_reset_key   '111222333444'
end

And my tests run fine.

I notice that on the factory girl page they define factories in their examples without passing a parameter after the do (http://github.com/thoughtbot/factory_girl):

# This will guess the User class
FactoryGirl.define :user do
  factory :user do
    first_name 'John'
    last_name  'Doe'
    admin false
  end

  # This will use the User class (Admin would have been guessed)
  factory :admin, :class => User do
    first_name 'Admin'
    last_name  'User'
    admin true
  end

  # The same, but using a string instead of class constant
  factory :admin, :class => 'user' do
    first_name 'Admin'
    last_name  'User'
    admin true
  end
end

In effort to clean up my code, I've tried to emulate defining factories without passing a parameter after the do and changed my code to:

Factory.define :user do
  factory :user do
    name       'Some guy'
    sequence(:email) {|n| "person#{n}@example.com" }
    password   'password'
  end

  factory :password_reset_user, :parent => :post do
    password_reset_key   '111222333444'    
  end
end

But I get the error:

undefined method `factory' for main:Object (NoMethodError)
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:56:in `define'
    from /web/votropolis/spec/factories.rb:1
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:227:in `load_dependency'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:307:in `find_definitions'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:305:in `each'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:305:in `find_definitions'
    from /Library/Ruby/Gems/1.8/gems/factory_girl_rails-1.0/lib/factory_girl_rails/railtie.rb:11
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:34:in `call'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:43:in `run_load_hooks'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application/finisher.rb:46
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `instance_exec'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `run'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:50:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `each'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:134:in `initialize!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:77:in `send'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:77:in `method_missing'
    from /web/votropolis/config/environment.rb:5
    from /web/votropolis/spec/spec_helper.rb:3:in `require'
    from /web/votropolis/spec/spec_helper.rb:3
    from /web/votropolis/spec/controllers/organizations/memberships_controller_spec.rb:1:in `require'
    from /web/votropolis/spec/controllers/organizations/memberships_controller_spec.rb:1
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `map'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:18:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:46:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:10:in `autorun'
    from /Library/Ruby/Gems/1.8/bin/rspec:19

What am I doing wrong? Or is it a compatibility issue -- i.e. perhaps factory_girl_rails (which is for rails 3) doesn't work the same way as factory_girl).

Thanks.

Sean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2024-10-06 18:12:18

这是因为本文档仅适用于 FactoryGirl beta。不是稳定版本。它在 README 之上进行了解释。

稳定的文档在这里: http://github.com/thoughtbot/factory_girl/tree/1.3 .x

It's because this documentation is only to FactoryGirl beta. Not stable version. It's explain on top of README.

The stable documentation is here : http://github.com/thoughtbot/factory_girl/tree/1.3.x

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文