I was reading Google Groups' Rails Group the other day when I ran into someone talking about why should I test the validates functions. Here is a quote from his email:
I just thought I would post my response for anyone who has the same question. Also, sorry if the format is bad or to quick, but I was trying to hurry when I wrote him the response.
Don't look it as testing Rails. You are testing your model on testing validates. Let's say you write this class:
class Person < ActiveRecord::Base
validates_length_of :phone_number, :within => 7..10
...
end
So you write a test:
def test_phone_number_length_correct
person = Person.new(:phone_number => '1234567')
person.valid?
assert !person.errors.invalid?(:phone_number), 'Valid Phone Number Coming Back as Invalid'
end
I think we would all agree that this test makes sure that a valid phone number passes.
Now not thinking you do a find and replace and replace all 7s with 8s or maybe number with numbers is more likely. So you have:
class Person < ActiveRecord::Base
validates_length_of :phone_numbers, :within => 7..10
...
end
Now you can place :phone_number => '12' into your database because you are now validating :phone_numbers.
If you have a test you run tests after you completed the find and replace, and this error would be caught right away. That is why you 'test Rails.'