Build Robust & Production Quality Applications - Lesson 2: Object Generation with Fabricators

It's typical to create many objects during testing and the more attributes you have the more cumbersome it could be to maintain, moreover, if an attribute changes, it then becomes very difficult to update all those tests.

To make this better, we can use an object generator framework called Fabrication.

Steps:

1. Create a folder under specs called "Fabricators"

2. create fabricator_todo.rb

Fabricator(:todo) do
    name { "cooking" }
  end

3. You can also create a user each time a todo is created:

Fabricator(:todo) do
  name { "cooking" }
  user { Fabricate(:user) }
end

or

Fabricator(:todo) do
  name { "cooking" }
  user
end

In your test suite:

If you want to create an object in your database:

describe "#display_text" do
  let(:todo) { fabricate(:todo) }
  subject { todo.display_text }

If you would rather create an object in memory, you could:

describe "#display_text" do
  let(:todo) { fabricate.build(:todo) }
  subject { todo.display_text }