Needs More Blink Tag Rotating Header Image

tattletale

Testing exception handling

You know the drill:
Or:
Or even:

I know, I know. “But I don’t do that!” Sure, you don’t. But what about that person in the Duluth office? You know, the one who still thinks that the whole project should be written in JSP and can’t wait to get all this done so he can go back to working on his port of “Leisure Suit Larry” for HP printers? I bet if you look hard, you’ll find a few exception handlers that are overly broad in scope, don’t provide sufficient context in their error message or just plain hide problems.

“But my tests!” you cry.  “Surely the finely crafted specs I’ve been working on will save me!” The problem is that overly aggressive exception handling in your code will mask the true cause of errors, resulting in a false sense of security. Sure, assert_nothing_raised and should_not raise never fire: your code is consuming the warnings and continuing blithely on its way.

This is why most ActiveRecord reference material emphasizes the user of find (vs. find_by_id), save! and the like, but doesn’t include begin and rescue blocks – it’s often better to have things blow up on you than guess at what error conditions might arise and attempt to handle them as you’re writing the code.  You can write tests around the exceptions, insuring that your code errors out in the appropriate manner when faced with bad input or exceptional conditions.

On the other hand, when something unexpected happens in production and your site starts giving everyone an eyeful of  your funny 500 page you’re unlikely to be so happy with all those uncaught exceptions.  Ah, the usual tension between development and production.  Enter tattletale.

tattletale is your hall monitor, your prison snitch: it tattles on all the exceptions raised during a particular test run, including messages, backtrace and the test that raised it. All of your existing exception handlers will work as expected, so your code doesn’t change. I’ve written a Tattler for Test::Unit for starters; extending to your favorite test framework should be as easy as writing your own Tattler. Grab the gem off github and take back your exception handling.

SEO Powered by Platinum SEO from Techblissonline