Posted Slides: How (Not To?) Test Validations

  • Posted By Stuart Halloway on July 17, 2007

I just finished giving my erubycon talk "Keeping Tests DRY." The slides are posted here. Pay special attention to slide 20. The code is probably not a good idea, but it accomplished my secondary goal of provoking a reaction from Jim Weirich.

Comments
  1. Gavin StarkJuly 17, 2007 @ 09:37 PM

    Great slides and it certainly has me thinking about some tests I’ve written.

    However, you seem to have lost something between the more explicit “test_name_validation” and “test_validates_presence_of.” The former also tests that the expected error message is received. Would you recommend modifying the parameters to test_validates_presence_of to include the expected error text, have a different meta-test helper or to drop that sort of testing altogether?

    Is your assertion in “decoupling from Rails?!?” that the tests above are testing both our use AND the implementation of the validation functions in Rails itself? While you note the code isn’t a good idea, it comes close to the interpretation of “this test should ensure that I’m enforcing presence validation on name and email” and avoids having to deal with trying to setup data that causes the Rails code path of that validation to execute. Perhaps that test is enough if we trust the implementation (and that there isn’t a plugin we installed that messes with it in some unexpected way.)

  2. Doug AlcornJuly 18, 2007 @ 01:49 AM

    Stu, I’m not sure it’s a good goal to gross out Jim with your code. ;-)