A library for setting up Ruby objects as test data
open sourceMITself-hostablefree
factory_bot replaces fixtures with factories: definitions that build valid test objects with sensible defaults, which each test overrides only where it matters. It has several build strategies (saved records, unsaved instances, attribute hashes and stubbed objects), several factories per class (user, admin_user) with inheritance. factory_bot_rails adds Rails integration. thoughtbot released it in 2008 as Factory Girl.
Problems it solves
- our test fixtures are a huge YAML file nobody understands
- create a valid user with all its required associations in one line in a test
- each test should only spell out the attributes it cares about
- make an admin and a regular user from the same definition
- generate unique emails for every test record
- build test objects without saving them to the database to keep tests fast
- set up complex test data for a Rails app in a readable way
Features
- create, build, attributes_for and build_stubbed strategies
- several factories per class with inheritance
- Rails integration (factory_bot_rails)
