Reputation: 8240
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.
Upvotes: 1
Views: 268
Reputation: 27747
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
Upvotes: 1