0
我有一個測試:Rails的測試失敗,夾具,但與直接的對象創建通過
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
我在做什麼毛病燈具?
我不希望價格是整數though..it應該是一個浮動 – Mark
你可以寫檢查你的病情,並通過自定義的驗證它來驗證。 –