我的枚举包括以下内容。
enum dv_type: { only_rental: 0, rental_buy: 1, only_one: 2 }
在我验证了enum之后,如果它是1和2,那么... ... price
将是> 0,`将等于0。
fatories/items.rb
price: 25000
dv_type { Item.dv_types.except(:only_one).keys.sample }
现在我想用affter(:build)按类型设值,应该怎么做?
type = 0 那么 price = 0
type = 1 and 2 then price > 0
工厂通常不应该包含随机数据。如果你总是返回dv_type的其中一个值,并使用traits来选择其他的值,那么你的测试套件将更加稳定。
然而,你所要求的很容易做到。你可以在你的工厂中引用其他列。
dv_type { Item.dv_types.except(:only_one).keys.sample }
price { dv_type.zero? ? 0 : 25000 }