How to convert multiple trait condition in Factory Girl to Fabricator?

Skip to first unread message

rakesh sukla

Mar 17, 2016, 4:37:51 PM3/17/16
to fabrication
Hi Everyone,

Could anyone tell me how can I convert the following function in Factory Girl to Fabricator? If I use inheritance then I have to define lot of fabricator which I really want to avoid. I will highly appreciate if someone can guide me

FactoryGirl.define do
factory :user do
first_name "Rakesh"
last_name "Sukla"

trait(:A) { credits 100 }
trait(:B) { credits 3300 }
trait(:C) { perks_user true }
trait(:F) { status 5 }
trait(:G) { status 0 }

Paul Elliott

Mar 17, 2016, 4:48:15 PM3/17/16
Hey Rakesh,

The short answer is that you'll need to specify different fabricators for these situations using inheritance.

That said, I do not believe you should include every possible permutation in your definitions. Generally your fabricators should specify a baseline set of valid objects based on the *business purpose* of each model. If you are testing specific scenarios that rely on certain fields you should set those fields explicitly in your tests.

In your case, I might make a couple of fabricators.

Fabricator(:user, alias: :new_user) do
  first_name { Faker::Name.first_name }
  last_name { Faker::Name.last_name }
  credits { rand(100) }
  status 0

Fabricator(:established_user, from: :user) do
  credits { rand(5000) }
  status 5

Readability of your tests and being explicit about what's important to this test are paramount. Looking at your Factories, you have some "magic numbers" that likely don't have special meaning but you are implying they do by specifying them like that.

-- Paul

You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Reply all
Reply to author
0 new messages