Friday, 13 July 2007

Assert no errors

Small snippet today... I've mainly been working on updating the test-coverage for our app the last couple of weeks - so nothing particularly useful to share. I have, however, found the following tiny test-helper method to be useful.

Many tests require you to check for a particular error on a specific field on an object... but you also tend to check (over, and over, and over) that there aren't any errors on a given object. So I extracted that out.

Put this into "test_helper.rb" for increased DRYness

  # Assert that the handed object (thing) has no errors on it.
  def assert_no_errors(thing)
    fail("should have passed an object") unless thing
    e = thing.errors.full_messages
    assert e.empty?, "Should not have error: #{e.to_sentence}"

No comments: