Show Menu

FactoryBot Cheat Sheet by

FactoryBot is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.

Defining factories

FactoryBot.define do

  # Default
  # It will use the User class
  factory :user do
    first_name { "John" }
    last_name { "Doe" }
    admin { false }

  # Specifying the class
  # It will use the User class, instead of Admin class
  factory :admin, class: User do
    first_name { "Admin" }
    last_name { "User" }
    admin { true }

Options (trans­ients)

factory :user do
  transient do
    upcased true

  after :create do |user, options|! if options.upcased
create(user, upcased: true)
Transient attributes will not get passed to the model, but will be available in after-­create hooks.

Nested Factories

factory :user do
  first_name 'John'

  factory :sample_user do
    first_name { Faker::Name.first_name }

create :sample_user


FactoryBot.define do
  factory :post do
    title { 'An awesome post' }
    body { 'Lorem Ipsum...' }

    trait :published do
      status { :published }

    trait :unpublished do
      status { :draft }
    trait :with_comments do
      after(:create) do |post|
        create_list :comment, 2, todo_item: post

# then in your test
let(:post) { create(:post, :published) }

# or even with
let(:post) { create(:post, :published, :with_comments) }
Trait helps you to remove duplic­ation.

Building factories

# Returns an User instance that's not saved
user =

# Returns a saved User instance
user = FactoryBot.create(:user)

# Returns a hash of attributes that can be used to build an User instance
attrs = FactoryBot.attributes_for(:user)

# Returns an object with all defined attributes stubbed out
stub = FactoryBot.build_stubbed(:user)

# Passing a block to any of the methods above will yield the return object
FactoryBot.create(:user) do |user|

# Overriding attributes of a factory
user =, first_name: "Joe")
# => "Joe"

# No matter which build strategy is used to override attributes
user = FactoryBot.create(:user, first_name: "Joe")
# => "Joe"


# If the factory name is the same as the association name, it's simple
factory :post do

# You can also specify a different factory or override attributes
factory :post do
  # ...
  association :author, factory: :user, last_name: "Writely"

# Builds and saves a User and a Post
post = FactoryBot.create(:post)
post.new_record? # => false # => false

# Builds and saves a User, and then builds but does not save a Post
post =
post.new_record? # => true # => false

Dependent attributes

factory :user do
  first_name { "Joe" }
  last_name { "Blow" }
  email { "#{first_name}.#{last_name}".downcase }
Attributes can be based on the values of other attrib­utes.


factory :user, aliases: [:author, :commenter] do
  first_name { "John" }
  last_name { "Doe" }
  date_of_birth { 18.years.ago }

factory :post do
  # instead of association :author, factory: :user
  title { "How to read a book effectively" }
  body { "There are five steps involved." }

factory :comment do
  # instead of association :commenter, factory: :user
  body { "Great article!" }
Aliases allow to use named associ­ations more easily.

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Cypressio Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet