Testing error handling
Posted: Wed Nov 01, 2006 5:21 pm
Hi all,
I've been a bit lazy in my TDD recently, and have been developing elements of my Admin Control Panel without testing. I am in the process of rewriting it, this time with a test first for each feature before I add the code I've already written.
One thing I've noticed is the way I handle errors. In my existing code, which is in a hand-made MVC format, an error is raised using trigger_error. A custom error handler stores the error in the $_SESSION array and outputs a location header to redirect to a pre-determined page, based on the process which threw the error. In writing tests for the error handling of certain functions, I notice when an error is thrown, the flow of the script is NOT halted by the redirect, but instead it continues because of Simpletest's own error handler. So I've had to update all my trigger_error calls to also include a return false; afterwards, in order that the process be terminated.
I'd just like to hear any thoughts you have on this subject - what is considered best practice when using trigger_error? Should I think about overriding the Simpletest error handler, or should I be more thorough about the way I trigger errors to make sure the classes work under test conditions?
Thanks!
I've been a bit lazy in my TDD recently, and have been developing elements of my Admin Control Panel without testing. I am in the process of rewriting it, this time with a test first for each feature before I add the code I've already written.
One thing I've noticed is the way I handle errors. In my existing code, which is in a hand-made MVC format, an error is raised using trigger_error. A custom error handler stores the error in the $_SESSION array and outputs a location header to redirect to a pre-determined page, based on the process which threw the error. In writing tests for the error handling of certain functions, I notice when an error is thrown, the flow of the script is NOT halted by the redirect, but instead it continues because of Simpletest's own error handler. So I've had to update all my trigger_error calls to also include a return false; afterwards, in order that the process be terminated.
I'd just like to hear any thoughts you have on this subject - what is considered best practice when using trigger_error? Should I think about overriding the Simpletest error handler, or should I be more thorough about the way I trigger errors to make sure the classes work under test conditions?
Thanks!