如何在"setUp"方法Xcode 6中进行单元测试的异步过程(例如,登录)

Mih*_*hir 7 xcode unit-testing asynchronous objective-c xcode6

我正在寻找在setup或teardown方法中使用XCTextExpectation的东西.在Objective-C中,它看起来像这样:

- (void)setUp {

XCTestExpectation *getUserAsyncComplete = [self expectationWithDescription:@"Get Request Attempted and Finished"];

[Connection loginWithEmail:@"some@email.com" onSuccess:^void(^) { 
     [getUserAsyncComplete fulfill];
} onError:nil;

[self waitForExpectationsWithTimeout:[self.timeLimit doubleValue] handler:^(NSError *error) {
    if (error != nil) {
        XCTFail(@"Failure: user retrieval exceeded %f seconds.", [self.timeLimit doubleValue]);
    }
}];
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过这段代码而且似乎没有工作; 这可能是因为Xcode 6仍然处于测试阶段,或者它不受支持.即使在Swift中有解决方案,也会非常感激.

Sas*_*ats 11

你错误输入了方法名称,这似乎工作得很好(Xcode 6,beta 2)

- (void)setUp {
    [super setUp];

    XCTestExpectation *exp = [self expectationWithDescription:@"Login"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(29 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [exp fulfill];
    });

    [self waitForExpectationsWithTimeout:30 handler:^(NSError *error) {
        // handle failure
    }];
}

- (void)testExample {
    XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
}
Run Code Online (Sandbox Code Playgroud)