我需要在我的模型来验证数据,然后使用RSpec的测试这种模式时失败测试寿模型这样验证使用validates_presence_of和validates_numericality_of
it { should validate_presence_of :sales_price }
it { should validate_presence_of :retail_price }
it { should validate_numericality_of :sales_price }
it { should validate_numericality_of :retail_price }
我得到这个错误
Failure/Error: it { should validate_presence_of :retail_price }
ArgumentError:
comparison of Float with nil failed
# ./spec/models/offer_option_spec.rb:19:in `block (2 levels) in <top (required)>'
我该如何解决这个问题?
也许另一种方式来看看它:你需要'validates_presence_of'?因为validates_numericality_of已经确保'retail_price'不是零。看起来很奇怪,它适用于'sales_price',但不适用于'retail_price'您使用什么值进行测试? – sled