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 if
is 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
精彩评论