我在 Xcode 6 (Beta 5) 中使用 XCTestExpectations 进行异步测试。每次我运行它们时,我所有的异步测试都会单独通过。然而,当我尝试运行我的整个套件时,一些测试没有通过,并且应用程序崩溃。
我得到的错误是 API violation - multiple calls made to -[XCTestExpectation fulfill]
.事实上,这在单一方法中是不成立的。我的测试的一般格式如下所示:
- (void) someTest {
/* Declare Expectation */
XCTestExpectation *expectation = [self expectationWithDescription:@"My Expectation"];
[MyClass loginOnServerWithEmail:@"example@email.com" andPassword:@"asdfasdf" onSuccess:^void(User *user) {
/* Make some assertions here about the object that was given. */
/* Fulfill the expectation */
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
/* Error handling here */
}];
}
同样,这些测试在单独运行时确实通过了,并且它们实际上是在发出网络请求(完全按预期工作),但是在一起时,测试集合无法运行。
我可以看看这篇文章here ,但无法获得适合我的解决方案。
此外,我正在运行 OSX Mavericks 并使用 Xcode 6(Beta 5)。
最佳答案
我不认为使用 __weak
或 __block
是一个很好的方法。我使用 XCTestExpectation
编写了许多单元测试有一段时间了,直到现在才遇到这个问题。我终于找到了问题的真正原因,这可能会导致我的应用程序出现错误。我的问题的根本原因是 startAsynchronousTaskWithDuration
多次调用 completionHandler。在我修复它之后,API 违规就消失了!
[self startAsynchronousTaskWithDuration:4 completionHandler:^(id result, NSError *error) {
XCTAssertNotNil(result);
XCTAssertNil(error);
[expectation fulfill];
}];
虽然我花了几个小时来修复我的单元测试,但我开始欣赏 API 违规错误,这将帮助我避免我的应用程序中 future 的运行时问题。
关于unit-testing - XCTestExpectation : API violation - multiple calls made to -[XCTestExpectation fulfill] 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25167223/