如何在调用completionHandler块之前使异步XCTestCase失败并导致测试崩溃?

时间:2015-11-04 19:29:46

标签: ios objective-c xctest xctestexpectation xctestcase

我正在使用XCTestCase进行iOS单元测试,因为"无法识别的选择器"我正在崩溃在异步Web服务处理程序上,它通过completionHandler块返回操作的NSDictionary。

崩溃不是我对断言语句(XCTAssertNoThrows)的期望,我想看到由于崩溃导致此断言的失败,但我编写的测试方法已停止执行并且在没有任何测试结果的情况下结束(通过/失败) 。

- (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);
        }
    }];
}

不幸的是,服务处理程序代码未被隔离以进行适当的单元测试。由于它不是孤立的,@ try ... @ catch没有捕获NSInvalidArgumentException而且它没用。很可能,我想实现顶级NSSetUncaughtExceptionHandler,但我不知道如何从NSSetUncaughtExceptionHandler失败相关的测试。

0 个答案:

没有答案