How to use XCTAssertThrowsSpecific How to use XCTAssertThrowsSpecific ios ios

How to use XCTAssertThrowsSpecific


You should only use exceptions for exceptional cases, not for error handling and flow control

Having said that, here's how you use XCTAssertThrowsSpecific:

XCTAssertThrowsSpecific expects the specific class of the exception as the second parameter. NSCAssert throws an NSException. To test for that, use

XCTAssertThrowsSpecific([object methodThatShouldThrow], NSException, @"should throw an exception");

Now, that won't help much, because it's likely that every exception is an NSException or a subclass thereof.

NSExceptions have a name property that determines the type of the exception. In case of NSCAssert this is NSInternalInconsistencyException. To test for that, use XCTAssertThrowsSpecificNamed

XCTAssertThrowsSpecificNamed(  [object methodThatShouldThrow],  NSException,  NSInternalInconsistencyException,  @"should throw NSInternalInconsistencyException");