Is it possible to define a default trait in FactoryGirl? If I define a factory like this (where both question_response belongs_to question):
factory :question_response do
question
work_history
trait :open do
question { FactoryGirl.create :question, question_type: 'open' }
end
end
When I do FactoryGirl.create :question_response, :open
it will first create a default question and then create another inside the trait, which is an unnecessary operation.
Ideally I'd like to do this:
factory :question_response do
work_history
trait :default do
question { FactoryGirl.create :question, question_type: 'yes_no' }
end
trait :open do
question { FactoryGirl.create :question, question_type: 'open' }
end
end
And then doing FactoryGirl.create :question
will use the default trait, but it doesn't seem to be possible.
Factory Bot (previously known as Factory Girl) is a library gem specific to Ruby. It creates test fixtures, these test fixtures can be used as a tool to help with automated testing. Factory Bot is intended to be more dynamic than the default fixtures that Rails has.
To do this we will use the Factory Bot gem which allows the developer to set default values for the instances of our class that we create. The methods Factory Bot provides are create, build, attributes_for, and build_stubbed. The two most important are the create and build.
build_stubbed imitates creating. It slubs id , created_at , updated_at and user_id attributes. Also it skips all validations and callbacks. Stubs means that FactoryBot just initialize object and assigns values to the id created_at and updated_at attributes so that it just looks like created.
When I do FactoryGirl.create :question_response, :open it will first create a default question and then create another inside the trait
It's not true. if you specify the trait with question
, it will overwrite the factory behavior before creation so that it does not create a default question.
I checked it with FactoryGirl v4.5.0
Just in case someone else is looking for 'default trait' scenario, this was discussed with examples in https://github.com/thoughtbot/factory_bot/issues/528#issuecomment-18289143
Basically, you can define trait with defaults and then use it for different factories. And then you can use appropriate factory with needed configuration.
For example:
FactoryBot.define do
trait :user_defaults do
email { Faker::Internet.unique.email }
username { Faker::Internet.unique.username }
password { "test1234" }
end
factory :with_admin_role, class: User do
user_defaults
after(:create) do |user, _|
user.add_role :admin
end
end
factory :with_readonly_role, class: User do
user_defaults
after(:create) do |user, _|
user.add_role :readonly
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With