- Published
- Author
- Mohammad hussainSystem Analyst
Traits in FactoryBot allow you to define reusable pieces of attributes that can be mixed into factories to create variations of objects without duplicating code.
Here,
You can pass traits to
#CU6U0R822 #RSpec
Code
FactoryBot.define do
factory :user do
name { "John Doe" }
trait :admin do
role { "admin" }
end
end
end
create(:user, :admin)Here,
:admin is a trait that overrides or adds attributes (role: "admin").You can pass traits to
create, build, or attributes_for to easily generate variations.#CU6U0R822 #RSpec