3

I'm trying to find an XCT macro that can assert that a method crashes, rather than an exception. For example, setting a nil key on an NSDictionary throws an exception, so you can test this with

XCTAssertThrows([[NSMutableDictionary dictionary] setObject:@"foo" forKey:nil], @"Setting a nil key on an NSMutableDictionary should throw an exception.")

However setting a nil key on NSUserDefaults doesn't seem to throw an exception, it just crashes the code with a line

Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

This looks like an exception breakpoint, but not a throw? Anyway, the following assertion will kill the test suite as noted above.

XCTAssertThrows([[NSUserDefaults standardUserDefaults] setObject:@"foo" forKey:nil], @"Setting a nil key on NSUserDefaults should crash.");
Ryan
  • 5,416
  • 1
  • 39
  • 36
  • Looks like [EXC_BREAKPOINT is related to SIGTRAP](http://stackoverflow.com/questions/2611607/are-exc-breakpoint-sigtrap-exceptions-caused-by-debugging-breakpoints). Maybe you can register a signal handler? – jscs Aug 04 '13 at 01:59
  • 4
    You shouldn't be spending time writing tests for Cocoa classes, but your own classes. What is your actual test case? – BergQuester Aug 04 '13 at 03:30
  • I'm not testing cocoa frameworks directly, that indeed would be a waste of my time. I simplified the example for the post. In reality I have a method which argument may be sent to either an NSMutableDictionary OR NSUserDefault object, which is why the difference in behaviour is problematic. My current solution is to check the parameter input and throw my own exception before sending to either instances. – Ryan Aug 04 '13 at 21:40

0 Answers0