0

我正在尝试设置 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时是个问题,因为你想做这样的事情:

Fabricator(:description) do |f|
  f.images(count: 1) { |description, i| Fabricate(:image, photo: description) }
end

但是 Fabricator-gem 自动假定必须有一个名为 Photo 的类。因此,当您运行测试时,会引发以下错误:

NameError:
       uninitialized constant Photo

我如何告诉 Fabricator 这是一个多态关联?

编辑:这里的堆栈跟踪:

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>'
4

1 回答 1

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

Fabrication 不会尝试将属性名称转换为类。只要您定义了 Fabricator(:image),最好没有photo关联,那么您应该能够做到这一点:

Fabricator(:description) do
  images(count: 1)
end
于 2013-01-22T21:34:24.827 回答