Rails 测试 - 如何测试特定的验证? [英] Rails Test - How to test specific validation?
本文介绍了Rails 测试 - 如何测试特定的验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用默认的 Rails 测试套件,我将如何编写仅测试某些验证的测试?例如,如果我有对存在、数字和我自己的验证器的验证,我如何只测试数字?
Using default Rails test suite, how would I write tests that test certain validations only? For example, if I have validations for presence, numeric, and my own validator, how do I just test numeric?
假设我没有数字验证工作,所以这个测试应该失败:
Say I don't have numeric validation working, so this test should fail:
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
但它会通过,因为我自己的验证器出于其他原因不允许它保存,所以我的测试并没有真正正确地测试.
But it'll pass because my own validator didn't allow it to save for some other reason, so my test doesn't really test properly.
推荐答案
您可以为特定字段断言 post.errors
哈希,即:
You coud assert the post.errors
hash for the specific field, i.e.:
assert_nil post.errors[:number], "it saved a non numeric value for :number"
这篇关于Rails 测试 - 如何测试特定的验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文