8

I want to define Fabricator for class has namespace like 'Foo::Bar'.
Tell me the way it can work.

Here my codes.

models/foo.rb

class Foo
  include Mongoid::Document
  embedded_in :foo_container, polymorphic: true

  field :xxx ....
end

models/foo/bar.rb

class Foo::Bar < Foo
  field :yyy ....
  field :zzz ....
end

data/fabricators/foo_bar_fabricator.rb

Fabricator(:foo_bar, class_name: 'Foo::Bar') do
   yyy 'MyString'
   zzz 'MyString'
end

When I tried to create Fabricatior object on parino console but error occurred.

> Fabricate(:foo_bar)
> NoMethodError: undefined method `new?' for nil:NilClass
  .... stack messages

When I tried to create other Fabricator object wasn't namespace class like 'User', it went right.

4

2 回答 2

6

根据 Fabrication 关于创建对象的文档:

要使用与类不同的名称,您必须指定from: :symbolized_class_name为第二个参数。

所以以下应该工作:

Fabricator(:foo_bar, from: 'Foo::Bar') do
  yyy 'MyString'
  zzz 'MyString'
end
于 2013-10-04T19:36:00.280 回答
2

这对我有用

Fabricator(:foo_bar, class_name: :'Foo::Bar') do
    xxx {Faker::Company.name}
    yyy 'Mystring'
end
于 2013-03-09T10:13:28.143 回答