Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS9 XCTest-如何使用期望来测试没有块的异步情况?_Objective C_Unit Testing_Ios9_Xctest_Xctestexpectation - Fatal编程技术网

Objective c iOS9 XCTest-如何使用期望来测试没有块的异步情况?

Objective c iOS9 XCTest-如何使用期望来测试没有块的异步情况?,objective-c,unit-testing,ios9,xctest,xctestexpectation,Objective C,Unit Testing,Ios9,Xctest,Xctestexpectation,我在看。它对于网络请求和其他具有可以满足预期的完成块的操作是有意义的 然而,我试图对不使用回调块的类进行单元测试。到目前为止,我发现下面的代码在某些时候是有效的,但我不确定我是否使用了正确的方法 当应用程序有机会在后台执行某项操作时,如何延迟测试用例?在这种情况下,我可以使用执行选择器在超时之前检查某项操作并满足该后台线程的期望吗 -(void)checkFoundExpectedSubview:(MockViewController*)mockViewController { if(m

我在看。它对于网络请求和其他具有可以满足预期的完成块的操作是有意义的

然而,我试图对不使用回调块的类进行单元测试。到目前为止,我发现下面的代码在某些时候是有效的,但我不确定我是否使用了正确的方法

当应用程序有机会在后台执行某项操作时,如何延迟测试用例?在这种情况下,我可以使用执行选择器在超时之前检查某项操作并满足该后台线程的期望吗

-(void)checkFoundExpectedSubview:(MockViewController*)mockViewController
{
    if(mockViewController.didFindExpectedSubview)
    {
        [self.expectation fulfill];
    }
}


-(void)testErrorMessage
{
    MockViewController* mockViewController = [MockViewController create];
    [mockViewController expectSubviewWithClass:@"TSMessageView"];

    NSString* title = @"Error!";
    NSString* subtitle = @"This is a unit test of an error message";

    [self.errorHandler reportErrorWithTitle:title message:subtitle];
//this checks and fulfills my expectation
    [self performSelector:@selector(checkFoundExpectedSubview:) withObject:mockViewController afterDelay:2];

    self.expectation = [self expectationWithDescription:@"Waiting for the error to be presented"];

    [self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) {
        XCTAssertNil(error);
    }];
}
您可以使用等待一定的时间,或直到满足条件

time_t start = time(NULL);
while (![mockViewController checkFoundExpectedSubview] && time(NULL)-start < 5.0) {
    CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, NO);
}
XCTAssertTrue([mockViewController checkFoundExpectedSubview], "Expected something, didn't happen");
可以这样使用:

wait(5, [mockViewController checkFoundExpectedSubview], "Expected something, didn't happen");
wait(5, [mockViewController checkFoundExpectedSubview], "Expected something, didn't happen");