我正在寻找在设置或拆卸方法中使用 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]);
}
}];
}
我试过这段代码,但它似乎不起作用;这可能是因为 Xcode 6 仍处于测试阶段,或者它不受支持。即使有 Swift 中的解决方案,也将不胜感激。
最佳答案
你打错了方法名,这似乎工作得很好(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__);
}
关于objective-c - 如何在 "setUp"方法 Xcode 6 中使用异步过程(例如,登录)进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24347874/