How Not to Test Validations, Part 2

This is Part 2 of the preview for "Keeping Tests Dry" at next week's erubycon.

Several people offered improvements to the bad code from Part 1. Here is my own second cut, much better than the first:

  # Still tests ActiveRecord as well as your code, but at least
  # no exception handling
  # tests only one attribute
  # not (as) fragile
  def test_name_validation
    c = Contact.new
    assert(!c.valid?)
    assert_equal("can't be blank", c.errors['name'])
  end

This could still be a lot better. How would you DRY this up for use across a large codebase? (More to follow...)

Get In Touch