[英]How to test a validation with conditional?
How should I test a validation with a conditional like this: 我应该如何使用这样的条件测试验证:
validates :age, :numericality => true, :if => :age?
This is what I have for now: 这就是我现在所拥有的:
before(:each) do
@attr = { :age => "30" }
end
it "should require a age if present" do
Model.new(@attr.merge(:age => "foo").should_not be_valid
end
And the error message is: 错误消息是:
expected valid? to return false, got true
But doing this, the if
is not evaluated. 但是,这样做不会评估if
。
您是否尝试过:allow_nil => true
而不是:if
条件?
Have you actually written a method called "age?" 您是否实际编写了一种称为“年龄”的方法? ? ?
I think what you're trying to do is actually covered by: 我认为您要执行的操作实际上涵盖了:
validates :age, :numericality => true, :allow_nil => true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.