mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
a154e64da1
Declarations are another layer of abstraction between defining the factories via the DSL and compiling the factories and their attributes. Declarations know how to return their attribute(s), and running a factory compiles the declarations before building all attributes on the factory. This moves all the attribute compilation logic into one centralized location on the Factory instance, which means traits (and potentially other features down the road) can have individual attributes overridden within child factories or through FactoryGirl.modify Closes #205
52 lines
1.3 KiB
Ruby
52 lines
1.3 KiB
Ruby
require "spec_helper"
|
|
|
|
describe "modifying inherited factories with traits" do
|
|
before do
|
|
define_model('User', :gender => :string, :admin => :boolean, :age => :integer)
|
|
FactoryGirl.define do
|
|
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
|
|
Factory.build(:male_user).gender.should == "Male"
|
|
end
|
|
|
|
it "returns the correct value for overridden attributes from traits defining multiple attributes" do
|
|
Factory.build(:female_user).gender.should == "Female"
|
|
Factory.build(:female_user).age.should == 25
|
|
Factory.build(:female_user).admin.should == true
|
|
end
|
|
|
|
it "allows modification of attributes created via traits" do
|
|
FactoryGirl.modify do
|
|
factory :male_user do
|
|
age 20
|
|
end
|
|
end
|
|
|
|
Factory.build(:male_user).gender.should == "Male"
|
|
Factory.build(:male_user).age.should == 20
|
|
Factory.build(:male_user).admin.should == true
|
|
end
|
|
end
|