Gerald Padgett E.
Gerald Padgett E.

Reputation: 51

How can I generate a FactoryGirl file into an specific folder?

I'm using rspec with factory_girl but when I run

rails g rspec:model catalog/product_type

I get this:

create spec/models/catalog/product_type_spec.rb

invoke factory_girl

create spec/factories/catalog_product_type.rb

but factory_girl doesn't generate the file in the catalog folder, instead it set catalog has part of the file name so I want to generate the factory_girl file just like it is generated in the spec file

Insted of this:

create spec/factories/catalog_product_type.rb

I want this

create spec/factories/catalog/product_type.rb

Attached file

Upvotes: 1

Views: 883

Answers (1)

Anthony E
Anthony E

Reputation: 11245

You can specify the default directory for your factories in your development.rb configuration file:

config.generators do |g|
  g.factory_girl dir: 'spec/factories/catalog/'
end

config.generators is part of Rails can is a common pattern for defining generator paths for other gems/libraries as well.

For more options on FactoryGirl generator config you can see the source here:

https://github.com/thoughtbot/factory_girl_rails/blob/master/lib/generators/factory_girl/model/model_generator.rb

And the docs:

https://github.com/thoughtbot/factory_girl_rails#configuration

Upvotes: 2

Related Questions