Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can FactoryBot generate factories after your models have been created?

When including the factory_bot_rails gem in your dev and test blocks in Gemfile, rails will generate factories automatically when your models are generated.

Is there a way to generate factories after your models have been generated?


Note: FactoryBot was previously named FactoryGirl

like image 729
GuiDoody Avatar asked Jul 28 '12 15:07

GuiDoody


People also ask

What is trait in factory bot?

FactoryBot's traits are an outstanding way to DRY up your tests and factories by naming groups of attributes, callbacks, and associations in one concise area. Imagine defining factories but without the attributes backed by a specific object.


1 Answers

First thing, look at the source project to find out how it was implemented:

https://github.com/thoughtbot/factory_bot_rails/blob/master/lib/generators/factory_bot/model/model_generator.rb

After that, try to guess how it works:

rails g factory_bot:model Car name speed:integer 

The result is:

create  test/factories/cars.rb 

And the content:

# Read about factories at https://github.com/thoughtbot/factory_girl  FactoryBot.define do    factory :car do      name "MyString"      speed 1    end end 

Remember, when you use rails g, you can always undo it, with rails d

rails d factory_bot:model Car name speed:integer 

Note: FactoryBot was previously named FactoryGirl

like image 105
Eduardo Santana Avatar answered Oct 07 '22 15:10

Eduardo Santana