Dealing with errors

Dealing with errors that occur in a test is always a tricky subject. At the very least you want to report the error. Sometimes you also want to do some processing to recover or do some clean up.

Exceptions

All Simon’s the macros throw exceptions if they encounter a problem. Simon automatically catches these and reports on them in it’s final report. Simon also makes use of a hierarchy of custom exceptions so that you can readily identify what went wrong. Here’s the list:

  • NSException
    • SIAbstractException – parent of all Simon’s custom exceptions.
      • SISyntaxException – thrown when there is a syntax error in a UI query.
      • SIUIException – general exception for UI related problems.
      • SIUINotFoundException – thrown by any macro which expects a single UIView from a query and does not find one.
      • SIUITooManyException – thrown by any macro which expects a single UIView from a query and more than one is returned.

Simon will also pick up on any other exceptions that are thrown. So the best way to get something reported on is to throw an exception.

Apple error processing recommendations

If you look at Apple’s error processing recommendations you will see that Apple recommends using NSErrors and error processing where ever possible rather than exceptions. Simon uses exceptions for dealing with tests because it can then catch a wider range of problems and it makes the code a lot simpler. Internally though Simon, uses NSErrors rather than exception where ever it can. This helps to separate internal error processing from test code errors.

So now lets take a look at a simple piece of code illustrating how this can be done:

SIMapStepToSelector(@"Then do something interesting", doSomething);
-(void) doSomething {
	@try {
		UIView *view = SIFindView(@"//UIButton");
	}
	@catch (SISyntaxException e) {
		NSLog(@"Syntax error");
		@throw e;
	}
	@finally {
		// here is where you can place clean up. perhaps to reset app.
	}
}

Here we show several points of interest. The first is the catch for the SISyntaxException. Honestly it’s unlikely that you would want to catch this, but it’s a good illustration of how you could catch an exception. Just be sure to re-throw the exception so that Simon knows there was a problem. Also notice the @finally block which is an idea place for putting any clean up code you might want to run.

blog comments powered by Disqus

Index

Simon What is BDD? Why Simon?
Installation Quick Start Guide Simon's UI
Writing Stories Mapping Stories Step Conversations Validating Results Accessing your app's UI Exceptions and Errors
The Pieman
Macro reference API reference Simon's CLI args Pieman's CLI args Change Log Thank you BSD License

Download latest
static library
v0.2.0