mardi 15 mars 2016

FactoryGirl Transient attributes? What are are they and why?

I read this from Thoughtbot but it's still confusing to me.

This is their example:

factory :user do
  transient do
    rockstar true
    upcased  false
  end

  name  { "John Doe#{" - Rockstar" if rockstar}" }
  email { "#{name.downcase}@example.com" }

  after(:create) do |user, evaluator|
    user.name.upcase! if evaluator.upcased
  end
end

create(:user, upcased: true).name
#=> "JOHN DOE - ROCKSTAR"

So,

  1. Is .upcased a real attribute on the model?
  2. What is the transient block really doing? Setting variables that can then be used in the factory?
  3. What is evaluator? Does it always need to be passed last? What if your create function uses traits, transients, and has multiple values?

Aucun commentaire:

Enregistrer un commentaire