【发布时间】:2014-09-29 19:00:45
【问题描述】:
我在 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)。
【问题讨论】:
-
在使用基本相同的格式之前,我已经运行了 100 多个测试套件,并且之前没有遇到过这个问题......你确定没有单独的测试用例有两个完成?
-
或许升级到 beta 6 看看问题是否依然存在?
-
@Mihir 当我在 expectForNotification 处理程序中调用完成时遇到了这个问题。我的假设是您的测试多次调用完成。如果您在调用完成时添加日志语句,您将看到:)
-
我之前也遇到过
API violation - multiple calls made to错误,但后来我意识到我错过了这个wait(for: [promise], timeout: 10)函数。但是你确实添加了waitForExpectations。
标签: unit-testing asynchronous ios8 xcode6 xctest