What are factory_girl transient attributes? Why would I use one?

我与影子孤独终老i 提交于 2020-01-01 02:00:46

问题


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?

回答1:


factory_bot's transient 'attributes' aren't attributes at all; they're just parameters to the factory method call that can be used by your code inside the factory. So, in your example, no, upcased isn't a model attribute.

The transient block lists 'attribute' names (that is, keys in the hash passed to the factory method) that are not attributes. factory_bot ignores them when setting attributes on the newly created model instance unless you write code in the factory definition to tell factory_bot to do something with them.

evaluator is an object passed to factory_bot callbacks. It's always the second block parameter; the model object is always the first parameter. It's conceptually like Ruby's binding. You can ask it for the value of any key in the argument hash, regardless of whether it's an actual attribute or a transient 'attribute'.

Traits and transient attributes don't affect each other as far as arguments to factory methods are concerned, since traits are scalar and transient attributes are part of the argument hash. Any number of real attributes and transient 'attributes' can be in the argument hash.

Here's the factory_bot documentation for the record: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md



来源:https://stackoverflow.com/questions/36021017/what-are-factory-girl-transient-attributes-why-would-i-use-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!