0

I am doing iOS unit testing using XCTestCase and I am getting a crash due to "unrecognized selector" on async web service handler which returns manipulated NSDictionary by means of completionHandler block.

The crash is not my expectation for assertion statement (XCTAssertNoThrows) and i want to see the failure for this assertion due to crash but the test method which i have written is stopped execution and ends without any test result (Pass/Fail).

- (void)testMethod
{
    XCTestExpectation *expectation = [self expectationWithDescription:@"Task longer than timeout"];

    [serviceHandler getInfoCompletionHandler:^(NSDictionary *infoDict){

        NSString *value = infoDict[@"Key"];
        XCTAssert(value.length > 0, @"Value should not be empty.");
        [expectation fulfill];

    } failure:^(NSError *error){

    NSLog(@"Error: %@",error);
        [expectation fulfill];

    }];

    [self waitForExpectationsWithTimeout:5 handler:^(NSError *error){
        if (error) {
            NSLog(@"Error: %@",error);
        }
    }];
}

Unfortunately, the service handler code is not isolated to do proper unit testing. Since it is not isolated, @try...@catch is not catching NSInvalidArgumentException and it is useless. Mostly likely, i want to implement top level NSSetUncaughtExceptionHandler, but i don't know how to fail the associated test from NSSetUncaughtExceptionHandler.

The Debugger
  • 330
  • 8
  • 19
  • 1
    XCTAssertTrue([object respondsToSelector:@selector(selector)]); – Alex Nov 04 '15 at 20:41
  • @Alex i understand this helps me to catch "unrecognized selector" problem, but in my case, i am not the owner of "object" directly which you have mentioned in your statement. Consider that "serviceHandler" which i have mentioned is the owner. – The Debugger Nov 05 '15 at 06:22
  • Then you need to fix serviceHandler to not crash, if it's part of a framework, or you can't alter the code for some reason then considering using a different service. – Alex Nov 05 '15 at 13:37

0 Answers0