...

/

Basic Factory Definition

Basic Factory Definition

Learn to write an example for factory_bot.

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:

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

and this for projects:

Press + to interact
FactoryBot.define do
factory :project do
name { "Project Runway" }
due_date { "1.week.from_now" }
end
end

Factories readability

...