Occasionally, somebody recommends or asks about using FactoryBot to generate seed data for an application. We recommend against it for a few reasons:
- The default attributes of a factory may change over time. This means
that, when you’re using FactoryBot to generate data in
seeds.rb, you’ll need to explicitly assign each attribute in order to ensure that the data is correct, defeating the purpose.
- Attributes may be added, renamed, or removed. This means your seeds file
will always need to be up to date with your factories as well as your
database schema. You likely won’t be running
rake db:seedevery time you change a migration. So, your seeds file may become out of sync and it won’t be immediately obvious. You’ll likely notice a breakage when a new developer comes onto the project.
- Data will still need to be checked for presence before insertion. The ActiveRecord gem gives you this with the “find or create” methods to locate a record or create it if it can’t be found. In addition, those methods will basically force you to manually define each attribute you want assigned, making FactoryBot unnecessary.
If you found this useful, you might also enjoy:
- FactoryBot Callbacks
- FactoryBot Traits
- Use FactoryBot’s
build_stubbedfor a Faster Test Suite
- Test-Driven Rails, taught by me
Looking for FactoryGirl? The library was renamed in 2017. Project name history can be found here.