factory_girl:我们可以检查一个属性是否已在其惰性属性块中被覆盖?

时间:2015-07-03 11:33:46

标签: ruby factory-bot

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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)

我不太了解你的示例代码。块中的代码已经知道数量是否被覆盖,否则块将无法运行!