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

标签 objective-c xcode unit-testing asynchronous xcode6

我正在寻找在设置或拆卸方法中使用 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/

相关文章:

ios - 滑动删除会显示应隐藏在单元格中的标签

xcode - 有人对新的 Xcode 10.3 更新有任何问题吗?

javascript - 测试-尝试使用 $httpBackend.expectGET 通过用户名获取用户 JSON 对象

php - 使用 PHPUnit 在 doctrine2 中模拟 findOneBy"field"

ios - SKSpritenode:物理质量不会因重力而改变加速度

objective-c - 当一个 Action 已经在进行时,用一个按钮开始一个 Action

ios - 具有完全自定义 getter 和 setter 的属性的 "weak"和 "copy"属性

ios - 将 Bundle 中的文件显示到 UITableView - iOS

ios - iOS 9 中 UILabel 中的多行文本

c# - 如何向 C# .NET Core 单元测试添加调试日志记录