Can FactoryBot generate factories after your models have been created? Can FactoryBot generate factories after your models have been created? ruby-on-rails ruby-on-rails

Can FactoryBot generate factories after your models have been created?


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_girlFactoryBot.define do   factory :car do     name "MyString"     speed 1   endend

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


The --fixture-replacement option will let you tell rails what to generate for building test data. You can set this as a default in your config/application.rb file, like so:

config.generators do |g|  g.fixture_replacement :factory_bot, suffix_factory: 'factory'end

Source: https://github.com/thoughtbot/factory_bot_rails/blob/master/features/fixture_replacement_config.feature#L21