【发布时间】:2018-03-11 20:25:14
【问题描述】:
我有许多异步单元测试,它们使用期望可以正常工作。但是,当我运行套装中的所有测试时,它们不会等待彼此完成 - 当下一个测试开始运行时,异步回调仍处于挂起状态。我想要的是每个测试在运行之前等待上一个测试中的期望。这些测试使用共享数据库,因此让它们重叠会导致烦人的额外复杂性,并且在作为套件运行时会导致测试失败。
- (void)testSignIn {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
XCTestExpectation *expectation =
[self expectationWithDescription:@"Expectations"];
[_userManager signInWithUsername:kUserEmail andPassword:kUserPassword
success:^{
XCTAssertNotNil([_userManager getCurrentUser]);
XCTAssertNotNil([_userManager getCurrentUser].plan);
XCTAssertTrue([_userManager getCurrentUser].plan.liveStream == TRUE);
[expectation fulfill];
} failure:^(EDApiError *apiError) {
XCTAssertTrue(FALSE); // Should not fail
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
if (error) {
NSLog(@"Timeout Error: %@", error);
}
}];
}
【问题讨论】:
-
这没有任何意义,一个测试应该在开始下一个测试之前失败/成功,无论哪种方式都必须满足期望。我唯一能想到的是
testSignIn是从单个测试的 2 个位置调用的。 1. 所以请确保你没有在一次测试中运行两次。 2.另外5秒超时有点太少了。我建议至少 30 秒。 -
看起来期望需要有不同的描述字符串。我将所有我的设置都设置为相同的字符串,这似乎导致了我所看到的行为。
-
(删除了我之前的评论)我试图理解这一点。我的意思是仍然没有 2 个测试同时运行。所以即使这有效,我猜这不是根本原因......
-
我自己还没有读完,但请看XCTestExpectation Gotchas
标签: ios xcode xctest xctestexpectation