[英]Rails test fails with fixture but passes with direct object creation
我有一个测试:
test 'gift should not be saved with a non-numeric price' do
@gift = gifts(:gift_without_numeric_price)
assert_not @gift.save, 'gift was saved with non-numeric name'
end
它使用以下夹具:
gift_without_numeric_price:
name: "pinecones"
description: "these pinecones smell really good"
estimated_price: "object"
link: "www.google.com"
我的Gift
模型如下所示:
validates :estimated_price,
presence: true,
numericality: true
因此,我期望测试能够通过,因为“对象”不是数字,导致@gift.save
返回false,并导致assert_not
最终返回true。 但是,由于某种原因,测试失败。 当我使用直接创建礼物对象的方式时,测试看起来像这样,测试通过:
test 'gift should not be saved with a non-numeric price' do
@gift = Gift.new(name: 'aa', description: 'description', link: 'www.google.com', estimated_price: 'object')
assert_not @gift.save, 'gift was saved with non-numeric name'
end
我的灯具在做什么错?
您可以尝试更改验证:
validates :estimated_price, presence: true,numericality: { only_integer: true }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.