2016-12-11 35 views
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 

我在做什麼毛病燈具?

回答

0

你可以試着改變你的驗證:

validates :estimated_price, presence: true,numericality: { only_integer: true } 
+0

我不希望價格是整數though..it應該是一個浮動 – Mark

+0

你可以寫檢查你的病情,並通過自定義的驗證它來驗證。 –

相關問題