【发布时间】:2017-07-04 22:23:28
【问题描述】:
考虑以下单元测试:
- (void)testSample {
XCTestExpectation *expectation = [self expectationWithDescription:@"Sample"];
[self.manager loadAllSuccess:^{
[expectation fulfill];
NSArray *data = [self.manager dataForIndex:0];
// Correct count of data is 10, not 44 - this should fail.
XCTAssertEqual(44, data.count);
} failure:^(NSError *error) {
[expectation fulfill];
XCTFail(@"Error encountered");
}];
[self waitForExpectationsWithTimeout:60 handler:nil];
}
我遇到了一些与已知失败案例有关的问题。尽管数据数组中应该只有 10 个项目,但测试成功完成。
如果我将 [expectation fulfill] 调用放在块的底部,在 XCTAssertEqual(44, data.count) 方法调用之后,测试会按预期工作并失败,直到我将值更正为 10。
这是一个已知问题吗?我一直无法阅读文档说我应该在最后一刻打电话给...
【问题讨论】:
-
总是在异步回调结束时调用fulfill()——提前满足期望可以设置一个竞争条件,运行循环可能在完成测试之前退出。如果测试有多个期望,它不会通过,除非每个期望在 waitForExpectationsWithTimeout() 指定的超时时间内执行fulfill()。在nshipster.com/xctestcase 找到这个
标签: ios unit-testing xctest xctestexpectation