开发者

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" d开发者_如何学Pythono
   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 ifis not evaluated.


Have you tried :allow_nil => true instead of the :if condition?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜