ruby on rails - What is the purpose of a `transient do` block in FactoryGirl factories? -
what purpose of transient do
in factorygirl factories?
i've seen lot of factories begin below.
factory :car owner nil other_attribute nil end ...
i've found information on blog: http://blog.thefrontiergroup.com.au/2014/12/using-factorygirl-easily-create-complex-data-sets-rails/
but still don't understand how , why this. experience factorygirl minimal.
could experience using factorygirl share insight?
transient
attributes allow pass in data isn’t attribute on model.
say have model called car
following attributes:
- name
- purchase_price
- model
you want capitalize name of car when create car model in factory. can is:
factory :car transient # capitalize not attribute of car capitalize false end name { "jacky" } purchase_price { 1000 } model { "honda" } after(:create) |car, evaluator| car.name.upcase! if evaluator.capitalize end end
hence, whenever create car factory , want capitalize name. can
car = factorygirl.create(:car, capitalize: true) car.name # => "jacky"
hope helps.
Comments
Post a Comment