【发布时间】: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
我需要知道工厂调用者是否为属性(惰性)块中的属性(覆盖)提供了一个值。当某些属性相互依赖时会非常有用。
factory :shipping_item do
quantity { % here, I would need to know if 'quantity' was overridden % }
end
【问题讨论】:
标签: ruby factory-bot
答案是:不支持/公共 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)
不过,我不太了解您的示例代码。块内的代码已经知道数量是否被覆盖,否则块将不会运行!
【讨论】: