Here’s a little snippet that I use to quickly form tests to ensure that my models validation is working correctly from within unit tests:

# In test_helper.rb   
# Give this function an array of invalid values, an array of valid values, the object and   
# attribute name to test, and it will ensure that only valid values are allowed   
def validation_tester ( valid_values, invalid_values, attribute_name, test_object )
  valid_values.each do |val|       
    test_object.[]=(attribute_name, val)
    assert test_object.save, "#{attribute_name} should be valid: #{test_object.[](attribute_name)}"       
    assert test_object.valid?
    assert !test_object.errors.invalid?(attribute_name)
  end

  invalid_values.each do |val|
    test_object.[]=(attribute_name, val) 
    assert !test_object.save, "#{attribute_name} should NOT be valid: #{val}" 
    assert !test_object.valid? 
    assert test_object.errors.invalid?(attribute_name)
  end   
end

Then, use it as follows in your unit tests:

def test_email_validation 
  valid_emails = ['matt@test.com', 'matt.hall@test.com', 'matt@test.ing.com', 'matt.hall@test.ing.com']
  invalid_emails = ['@test.com', 'test.com', 'matt@', 'matt.hall@com', '.matt@test.ing.co.uk', 'matt@@test.com', '', '.@.']
  u = create_user
  validation_tester( valid_emails, invalid_emails, 'email', u)
end

Can anyone suggest any improvements?