ScriptsSep 11, 2026·3 min read

FactoryBot — Test Data Factory for Ruby

A Ruby library for setting up test data objects with a clean, flexible DSL that replaces brittle fixture files with dynamic factory definitions.

Agent ready

Ready-to-run agent install

This asset can be installed after the agent chooses its runtime, checks the plan, and runs the matching command.

Native · 98/100Policy: allow
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: Established
Entrypoint
FactoryBot Overview
Direct install command
npx -y tokrepo@latest install 96ee885f-ae2e-11f1-9bc6-00163e2b0d79 --target codex

Run after dry-run confirms the install plan.

Introduction

FactoryBot is a Ruby library by thoughtbot that replaces static YAML fixtures with dynamic factory definitions. It generates test objects with sensible defaults, traits, and associations, making test setup concise and the data easy to customize per test case.

What FactoryBot Does

  • Defines reusable object templates with default attribute values
  • Generates unique values using sequences for fields like email and username
  • Supports traits for named variations of a factory (admin, inactive, premium)
  • Builds object graphs with nested associations automatically
  • Offers build, create, build_stubbed, and attributes_for strategies

Architecture Overview

FactoryBot maintains a registry of factory definitions keyed by name. When you call create(:user), it looks up the factory, evaluates its attribute blocks in a clean scope, resolves any associated factories, and persists the object. Lazy attributes, sequences, and traits compose together so each test can override only what it needs while inheriting sensible defaults for everything else.

Self-Hosting & Configuration

  • Add factory_bot or factory_bot_rails to your Gemfile
  • Place factory definitions in spec/factories/ or test/factories/
  • Include FactoryBot::Syntax::Methods in your test helper for short syntax
  • Enable linting with FactoryBot.lint to catch broken factories early
  • Configure the default build strategy globally if needed

Key Features

  • Trait system for composable, named attribute sets
  • Transient attributes for passing parameters that control factory logic
  • Callback hooks (after_build, after_create) for additional setup steps
  • Inheritance so child factories extend parent definitions cleanly
  • Linting command that validates all factories can be created without errors

Comparison with Similar Tools

  • Rails fixtures — static YAML files that are fast but brittle and hard to customize per test
  • Fabrication — similar factory library with a slightly different DSL
  • Machinist — blueprints-based approach, less actively maintained
  • FactoryGirl — the previous name of FactoryBot before the rename
  • Faker — generates random realistic data but does not manage object creation

FAQ

Q: What is the difference between build and create? A: build instantiates the object in memory without saving it to the database. create persists it, which is needed when tests query the database.

Q: Can I use FactoryBot outside of Rails? A: Yes. The core factory_bot gem works with any Ruby project. The factory_bot_rails gem adds Rails-specific integration like automatic factory discovery.

Q: How do I avoid slow tests from excessive database writes? A: Use build_stubbed to create objects that behave like persisted records without touching the database, or use build when persistence is not needed.

Q: Does it work with RSpec and Minitest? A: Yes. FactoryBot is framework-agnostic and integrates with both RSpec and Minitest through a simple include statement.

Sources

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets