$ show thoughtbot/factory_bot
factory_bot is a Ruby gem that replaces fixtures in test suites by providing a DSL for defining factoriesβtemplates for generating model instances with configurable attributes, associations, sequences, and traits. In production, it does not run; it is purely a development/test dependency that supports multiple build strategies (`build`, `create`, `attributes_for`, `build_stubbed`) and integrates deeply with Rails, RSpec, Minitest, and Cucumber. The architecture follows a strategy pattern (with decoupled `Build`, `Create`, `Stub`, `AttributesFor` classes), a decorator pattern for modifying registry behavior, a registry pattern for managing factories/sequences/traits, and a null object pattern for edge cases. Key entry points are `FactoryBot.define`, `FactoryBot.build`, `FactoryBot.create`, and the `FactoryBot.lint` validator. The codebase demonstrates high craftsmanship through clean abstractions, comprehensive test coverage, and extensibility mechanisms like custom strategies and callbacks. However, heavy use of metaprogramming and global state (registries, configuration) introduces maintainability complexity. Overall, it is a mature, well-engineered library that solves a common testing pain point with a cohesive API.
The LLM identified 3 files worth highlighting for reuse:
Implements the `build` strategy for unsaved model instances. The class defines a minimal interface (`association` and `result` methods) that makes it interchangeable with other strategies. It is self-contained, depends only on the `FactoryBot::Strategy` base class, and can be extracted as a generic build strategy for any object construction pattern.
A generic key-value registry class with `find`, `register`, and `clear` methods, enhanced with `did_you_mean` suggestions on lookup failures. It is decoupled from factory bot internals (only requires `did_you_mean` gem integration) and could be reused in any Ruby project needing a self-documenting registry with spelling correction.
A simple null object implementation that returns itself for all method calls, used to avoid nil checks in factory attribute resolution. It is fully reusableβextract it to any project needing a bottomless null object that delegates composition or equality checks gracefully.