I run into this question each time I add a validation to my model.
Should I add a unit test for that validation?
On the one hand, I’ve heard/read the philosophy: “Q: What should I
test? A: Only the stuff you care about working”.
On the other hand I’ve heard/read, “You don’t need to test methods
provided by Rails – they are already tested by the test suite
included with the framework.”
If I put this in my model:
Then I feel like I should write a unit test like
test_name_should_be_unique. But then that feels like I’m testing the
framework. If I’ve gone through the trouble of adding the validation
to my model, then it feels like I should go through the trouble of
testing the code I added to my model. Equivalently, if you’re in the
TDD/BDD driven camp, if I care about the behavior that the name should
be unique, I should write a test in which I try to add 2 records with
the same name field, verify the test fails, add the validation to my
model, and verify the test passes.
At this point, I start to think… DRY. Why should I write
substantially the same tests for every field in each model for which I
care that the field is unique (or present, or has a numericality,
Are there test helper methods such as #test_uniqueness_of, or
#test_presence_of? I’ve never seen such functions, which makes me
think that they’re not important or useful enough for anybody (other
than myself) to have written and used. Since I have never had a good
idea that wasn’t replicated 100 times on the internet already, I tend
to think these sort of helper functions aren’t a good idea.
So I ask why not? What am I missing?
You received this message because you are subscribed to the Google
Groups “Ruby on Rails: Talk” group.
To post to this group, send email to removed_email_[email protected]
To unsubscribe from this group, send email to
For more options, visit this group at