How to define Fabricator for namespace class
Asked Answered
V

2

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.

Vocable answered 30/1, 2013 at 8:44 Comment(0)
H
6

According to Fabrication's documentation on creating objects:

To use a different name from the class, you must specify from: :symbolized_class_name as the second argument.

So the following should work:

Fabricator(:foo_bar, from: 'Foo::Bar') do
  yyy 'MyString'
  zzz 'MyString'
end
Hephzipa answered 4/10, 2013 at 19:36 Comment(0)
R
3

This worked for me

Fabricator(:foo_bar, class_name: :'Foo::Bar') do
    xxx {Faker::Company.name}
    yyy 'Mystring'
end
Roe answered 9/3, 2013 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.