0
0
RailsHow-ToBeginner · 3 min read

How to Use Factory Bot in Rails for Easy Test Data Setup

Use FactoryBot.define to create factories for your Rails models, then call create(:factory_name) or build(:factory_name) in tests to generate test data. Factory Bot helps you write clean, reusable test setups without manually creating objects each time.
📐

Syntax

Factory definition: Use FactoryBot.define with a block to declare a factory for a model. Inside, use factory :name to name it and define attributes.

Usage in tests: Use create(:name) to save an instance to the database or build(:name) to instantiate without saving.

ruby
FactoryBot.define do
  factory :user do
    name { "Alice" }
    email { "alice@example.com" }
  end
end

# In tests:
user = FactoryBot.create(:user)  # saves user to DB
user = FactoryBot.build(:user)   # builds user without saving
💻

Example

This example shows how to define a User factory and use it in a Rails test to create a user with default attributes.

ruby
FactoryBot.define do
  factory :user do
    name { "Alice" }
    email { "alice@example.com" }
  end
end

# Usage in a test file (e.g., RSpec):
RSpec.describe User, type: :model do
  it "creates a valid user" do
    user = FactoryBot.create(:user)
    expect(user).to be_valid
    expect(user.name).to eq("Alice")
  end
end
Output
Finished in 0.01 seconds (files took 0.2 seconds to load) 1 example, 0 failures
⚠️

Common Pitfalls

  • Forgetting to include FactoryBot::Syntax::Methods in your test setup, which allows calling create and build without prefix.
  • Not saving the factory instance when you need it persisted (use create instead of build).
  • Defining factories with static attributes that cause duplicate unique constraint errors; use blocks to generate dynamic values.
ruby
  # Wrong: static email causes duplicates
  factory :user do
    email { "user@example.com" }
  end

  # Right: dynamic email to avoid duplicates
  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
  end
📊

Quick Reference

MethodDescription
FactoryBot.defineDefines a factory for a model
factory :nameNames the factory and sets attributes
create(:name)Builds and saves an instance to the database
build(:name)Builds an instance without saving
sequence(:attr)Generates unique values for attributes

Key Takeaways

Define factories with FactoryBot.define and use blocks for dynamic attributes.
Use create(:factory) to save test data and build(:factory) to instantiate without saving.
Include FactoryBot syntax methods in your test setup for cleaner calls.
Avoid static attributes that cause duplicate errors by using sequences.
Factory Bot simplifies and cleans up test data creation in Rails.