Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define Fabricator for namespace class

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.

like image 323
utwang Avatar asked Jan 30 '13 08:01

utwang


2 Answers

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
like image 197
Cameron Pope Avatar answered Oct 19 '22 08:10

Cameron Pope


This worked for me

Fabricator(:foo_bar, class_name: :'Foo::Bar') do
    xxx {Faker::Company.name}
    yyy 'Mystring'
end
like image 38
toxaq Avatar answered Oct 19 '22 08:10

toxaq