Factory Bot, originally known as Factory Girl, is a software library for the Ruby programming language that provides factory methods to create test fixtures for automated software testing. The fixture objects can be created on the fly; they may be plain Ruby objects with a predefined state, ORM objects with existing database records or mock objects.
Factory Bot is often used in testing Ruby on Rails applications; where it replaces Rails' built-in fixture mechanism. Rails' default setup uses a pre-populated database as test fixtures, which are global for the complete test suite. Factory Bot, on the other hand, allows developers to define a different setup for each test and thus helps to avoid dependencies within the test suite.
A factory is defined by a name and its set of attributes. The class of the test object is either determined through the name of the factory or set explicitly.
Traits allow grouping of attributes which can be applied to any factory.
Factory Bot allows creating aliases for existing factories so that the factories can be reused.
Factory Bot allows creating unique values for a test attribute in a given format.
Factories can be inherited while creating a factory for a class. This allows the user to reuse common attributes from parent factories and avoid writing duplicate code for duplicate attributes. Factories can be written in a nested fashion to leverage inheritance.
Parent factories can also be specified explicitly.
Factory Bot allows custom code to be injected at four different stages: