我的Rails应用程序中有两个模型:
class Item < ActiveRecord::Base
end
class ItemLocalization < ActiveRecord::Base
end
同样,我有两家工厂:
factory :item do
sequence(:name) { |n| "item #{n}" }
description "this is a description"
association :locale, :factory => :locale_deCH
item_group
end
factory :item_localization do
sequence(:name) { |n| "item #{n}" }
description "this is a description"
association :locale, :factory => :locale_deCH
item
user
reviewer
end
当有第二个带有_localization后缀的工厂时,FactoryGirl返回一个DuplicationError:
Rack::File headers parameter replaces cache_control after Rack 1.5.
/Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/decorator.rb:10:in `method_missing': Factory already registered: item_localization (FactoryGirl::DuplicateDefinitionError)
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/decorator/disallows_duplicates_registry.rb:6:in `register'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl.rb:65:in `block in register_factory'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl.rb:64:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl.rb:64:in `register_factory'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:20:in `factory'
from /Users/user/Sites/ec/spec/factories.rb:62:in `block in <top (required)>'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:49:in `run'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:7:in `define'
from /Users/user/Sites/ec/spec/factories.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:16:in `block in find_definitions'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl-4.1.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/factory_girl_rails-4.1.0/lib/factory_girl_rails/railtie.rb:26:in `block in <class:Railtie>'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:34:in `call'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:42:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/activesupport-3.2.11/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/railties-3.2.11/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/user/Sites/ec/config/environment.rb:5:in `<top (required)>'
from /Users/user/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/user/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/user/Sites/ec/spec/spec_helper.rb:3:in `<top (required)>'
from /Users/user/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/user/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/user/Sites/ec/spec/models/organization_spec.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
from /Users/user/.rvm/gems/ruby-1.9.3-p327@rails329-ec/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'
后缀"_localization"是否以某种方式保留?Rails似乎没有问题。
我发现了问题。它与"_localization"后缀无关。相反,在创建模型ItemLocalization时,Rails在test/filents文件夹中创建了一个同名的工厂,而我在rspec/filents下使用了相同的工厂。
从测试/工厂文件夹中删除所有文件解决了问题。