【问题标题】:factory_girl: Can we check if an attribute has been overridden in its lazy attribute block?factory_girl:我们能否检查一个属性是否在其惰性属性块中被覆盖?
【发布时间】:2015-07-03 11:33:46
【问题描述】:

我需要知道工厂调用者是否为属性(惰性)块中的属性(覆盖)提供了一个值。当某些属性相互依赖时会非常有用。

factory :shipping_item do
  quantity { % here, I would need to know if 'quantity' was overridden % }
end

【问题讨论】:

    标签: ruby factory-bot


    【解决方案1】:

    答案是:不支持/公共 API 方式。

    根据这个问题https://github.com/thoughtbot/factory_girl/issues/583,FactoryGirl作者不希望支持这个功能。

    虽然没有记录(因此更容易更改),但您可以访问实例变量 @overrides 中的覆盖参数。它是一个带有符号键的哈希。

    factory :shipping_item do
      quantity { @overrides[:other_attribute] || do_something_else }
    end
    
    # You can also even differentiate between the user
    # not supplying an override at all, or supplying nil
    # by checking @overrides.keys?(:other_attribute)
    

    不过,我不太了解您的示例代码。块内的代码已经知道数量是否被覆盖,否则块将不会运行!

    【讨论】:

      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 2012-05-03
      • 2016-09-04
      • 2012-02-12
      相关资源
      最近更新 更多