我正在尝试设置fabricator-gem来处理我的模型进行测试。我遇到的问题是它似乎无法处理多态关系。我使用MongoDB作为我的数据库,使用Mongoid作为我的ORM。考虑这个例子:
class Description
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :images, as: :photo, cascade_callbacks: true
end
class Image
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :photo, polymorphic: true
end
使用此代码,您可以将Image文档嵌入到许多不同类型的文档中,因为您设置了polymorphic: true
。这在使用制造商时会出现问题,因为您希望这样做:
Fabricator(:description) do |f|
f.images(count: 1) { |description, i| Fabricate(:image, photo: description) }
end
但Fabricator-gem自动假定必须有一个名为Photo的类。因此,在运行测试时,会引发以下错误:
NameError:
uninitialized constant Photo
如何告诉Fabricator这是一个多态关联?
编辑:Stacktrace:NameError: uninitialized constant PhotoOf
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:230:in `block in constantize'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:229:in `each'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:229:in `constantize'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/relations/metadata.rb:602:in `klass'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/relations/builders/nested_attributes/one.rb:33:in `build'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:170:in `block in process_relations'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:167:in `each_pair'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:167:in `process_relations'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:153:in `process_pending'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:32:in `block in process_attributes'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:193:in `with_mass_assignment'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/attributes/processing.rb:22:in `process_attributes'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/document.rb:148:in `block in initialize'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/mongoid-3.0.17/lib/mongoid/document.rb:143:in `initialize'
... 33 levels...
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:91:in `block in process_attributes'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:90:in `each'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:90:in `process_attributes'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:6:in `build'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/generator/base.rb:26:in `create'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/schematic/definition.rb:62:in `block in fabricate'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/schematic/definition.rb:61:in `instance_eval'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/schematic/definition.rb:61:in `fabricate'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication/fabricator.rb:10:in `fabricate'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/fabrication-2.5.4/lib/fabrication.rb:51:in `Fabricate'
from (irb):1
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /Users/max/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
答案 0 :(得分:1)
我认为问题实际上与您如何定义embeds_many关联有关。尝试完全删除“as”选项,看看是否能解决问题。
class Description
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :images, cascade_callbacks: true
end
class Image
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :photo, polymorphic: true
end
制作不会尝试将属性名称转换为类。只要你定义了一个Fabricator(:image),最好没有photo
关联,那么你应该能够做到这一点:
Fabricator(:description) do
images(count: 1)
end