我用这种方式定义了我的工厂:
Factory.define :user do |u| u.name 'Some guy' u.sequence(:email) {|n| "person#{n}@example.com" } u.password 'password'endFactory.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 classFactoryGirl.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 endend
为了清理我的代码,我试图模仿定义工厂而不在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' endend
但我得到错误:
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 – FactoryGirl:如何在不传递参数的情况下定义工厂全部内容,希望文章能够帮你解决ruby-on-rails – FactoryGirl:如何在不传递参数的情况下定义工厂所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)