2016-12-11 8 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 

それは、この治具を使用しています。

validates :estimated_price, 
     presence: true, 
     numericality: true 

gift_without_numeric_price: 
    name: "pinecones" 
    description: "these pinecones smell really good" 
    estimated_price: "object" 
    link: "www.google.com" 

Giftモデルは、このようになります

「オブジェクト」は数値ではないので、テストが成功すると予想していたので、@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

あるべき価格は整数になりたくありません検証する。 –

関連する問題