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.
According to Fabrication's documentation on creating objects:
To use a different name from the class, you must specify
from: :symbolized_class_nameas the second argument.
So the following should work:
Fabricator(:foo_bar, from: 'Foo::Bar') do
  yyy 'MyString'
  zzz 'MyString'
end
                        This worked for me
Fabricator(:foo_bar, class_name: :'Foo::Bar') do
    xxx {Faker::Company.name}
    yyy 'Mystring'
end
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With