Я использую 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).
Спасибо.
Шон