2012-01-18 33 views
3

使用默認的Rails測試套件,我將如何編寫僅測試某些驗證的測試?例如,如果我對存在,數字和我自己的驗證器進行了驗證,那麼如何測試數字?Rails測試 - 如何測試特定的驗證?

說我沒有數字的驗證工作,因此本次測試失敗了:

test "should test numeric validator" do 
    post = Post.new(:number => 'not a number') 
    assert !post.save, "it saved a non-numeric value for :number" 
end 

但它會通過,因爲我自己的驗證沒有允許它來保存一些其他原因,所以我測試並沒有真正地測試。

回答

4

您coud斷言post.errors散列的特定領域,即:

assert_nil post.errors[:number], "it saved a non numeric value for :number"