gpt4 book ai didi

ruby-on-rails - 工厂女孩 : How do define a factory without passing parameters

转载 作者:行者123 更新时间:2023-12-05 00:05:50 24 4
gpt4 key购买 nike

我正在使用 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

我的测试运行良好。

我注意到在 factory girl 页面上,他们在示例中定义了工厂,而没有在 之后传递参数。做 (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

为了清理我的代码,我尝试模拟定义工厂而不在 之后传递参数。做 并将我的代码更改为:
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 的工作方式不同。

谢谢。

肖恩

最佳答案

这是因为此文档仅适用于 FactoryGirl 测试版。不是稳定版。它是在 README 之上的解释。

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

关于ruby-on-rails - 工厂女孩 : How do define a factory without passing parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4043697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com