2011-09-23 13:14:02 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe "modifying inherited factories with traits" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('User', gender: :string, admin: :boolean, age: :integer)
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-09-23 13:14:02 -04:00
|
|
|
factory :user do
|
|
|
|
trait(:female) { gender "Female" }
|
|
|
|
trait(:male) { gender "Male" }
|
|
|
|
|
|
|
|
trait(:young_admin) do
|
|
|
|
admin true
|
|
|
|
age 17
|
|
|
|
end
|
|
|
|
|
|
|
|
female
|
|
|
|
young_admin
|
|
|
|
|
|
|
|
factory :female_user do
|
|
|
|
gender "Female"
|
|
|
|
age 25
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :male_user do
|
|
|
|
gender "Male"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct value for overridden attributes from traits" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:male_user).gender).to eq "Male"
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct value for overridden attributes from traits defining multiple attributes" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:female_user).gender).to eq "Female"
|
|
|
|
expect(FactoryBot.build(:female_user).age).to eq 25
|
|
|
|
expect(FactoryBot.build(:female_user).admin).to eq true
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows modification of attributes created via traits" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.modify do
|
2011-09-23 13:14:02 -04:00
|
|
|
factory :male_user do
|
|
|
|
age 20
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:male_user).gender).to eq "Male"
|
|
|
|
expect(FactoryBot.build(:male_user).age).to eq 20
|
|
|
|
expect(FactoryBot.build(:male_user).admin).to eq true
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
end
|