Search code examples
objective-cunit-testingxcode4gh-unit

GHAssertThrowsSpecific cannot find type NSRangeException


I'm using Xcode 4 and GHUnit to write some unit tests for the first time. All the advice appears to suggest going with GHUnit and not OCUnit.

I have a custom collection object called 'myList', and passing a message to get the selection at index:-1. It therefore correctly throws an NSRangeException (from the underlying mutable array).

I'm struggling to catch this with a GHAssertThrowsSpecific assertion.

This following line of code will not compile saying 'Unknown type name 'NSRangeException'.

GHAssertThrowsSpecific(s = [myList selectionAtIndex:-1],
            NSRangeException, @"Should have thrown an NSRangeException", nil);

I am #importing "Foundation/NSException.h" where NSRangeException appears to be defined. If I change it to:

GHAssertThrowsSpecific(s = [myList selectionAtIndex:-1],
            NSException, @"Should have thrown an NSException", nil);

then compiles fine and the assertion works, so its something to do with NSRangeException.

If I look in the headers, NSRangeException appears to be defined as a NSString * const in which case, how do I try to assert that I am expecting to catch it.

I'm obviously being quite dumb, as I can't see what I'm doing wrong.


Solution

  • Ok, so I found the answer to this one.

    NSRangeException is indeed just a pointer to a string, which contains "NSRangeException".

    Instead of using GHAssertThrowsSpecific, I should have been using GHAssertThrowsSpecificNamed, which takes an additional parameter of the string of the named exception, as follows:

    GHAssertThrowsSpecificNamed(s = [myList selectionAtIndex:-1],
      NSException, NSRangeException, @"Should have thrown an NSRangeException", nil);
    

    This works.