Search⌘ K
AI Features

Basic Factory Definition

Explore how to define basic factories using FactoryBot in your Rails tests. Understand setting default attributes, using dynamic values, and linking factory attributes to create efficient and readable test data.

All the definitions of our factories go inside a call to the method FactoryBot.define, which takes a block argument. Inside that block, we can declare factories to define default data. Each factory declaration takes its own argument in which we can define default values on an attribute-by-attribute basis.

A simple example for the task builder might look like this for tasks:

Ruby
FactoryBot.define do
factory :task do
title { "Thing to do" }
size { "1" }
completed_at { "nil" }
end
end

and this for projects:

Ruby
FactoryBot.define do
factory :project do
name { "Project Runway" }
due_date { "1.week.from_now" }
end
end

Factories

...