Question

I'm new with Unit Test and I've got question about it.

In my source code I have subclassed NSMutableArray. NSMutableArray must have implemented primitive NSMutableArray and NSArray methods. These methods in some case throws NSExceptions like NSRangeExceptions.

The question is: "When I make unit tests for this primitive methods should I write case for NSExceptions?

Example:

//array with 3 objects
STAssertThrows([array insertObject:object atIndex:8], @"");
  1. Is this correct?
  2. Is this neccesary?
Was it helpful?

Solution

There are two cases to consider: Asserting that an exception will be thrown, and asserting that an exception will not be thrown.

The latter is not necessary, but can still be a good idea. It's an opportunity to provide a more explicit description in the assertion message of what you were expecting to happen, or not to happen. But, you can skip STAssertNoThrow and the exception will still fail the test.

If you want the exception to happen, then you will need to assert that, like you did in the code in the question. You should provide a meaningful assertion message, though, rather than the empty string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top