2011-09-23 13:14:02 -04:00
|
|
|
describe "modifying inherited factories with traits" do
|
|
|
|
before do
|
2018-10-07 21:45:51 -04: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
|
2018-07-29 11:30:02 -04:00
|
|
|
trait(:female) { gender { "Female" } }
|
|
|
|
trait(:male) { gender { "Male" } }
|
2011-09-23 13:14:02 -04:00
|
|
|
|
|
|
|
trait(:young_admin) do
|
2018-07-29 11:30:02 -04:00
|
|
|
admin { true }
|
|
|
|
age { 17 }
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
female
|
|
|
|
young_admin
|
|
|
|
|
|
|
|
factory :female_user do
|
2018-07-29 11:30:02 -04:00
|
|
|
gender { "Female" }
|
|
|
|
age { 25 }
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
factory :male_user do
|
2018-07-29 11:30:02 -04:00
|
|
|
gender { "Male" }
|
2011-09-23 13:14:02 -04:00
|
|
|
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
|
2018-07-29 11:30:02 -04:00
|
|
|
age { 20 }
|
2011-09-23 13:14:02 -04:00
|
|
|
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
|