unit-testing - XCTestExpectation : API violation - multiple calls made to -[XCTestExpectation fulfill] 错误

标签 unit-testing asynchronous ios8 xcode6 xctest

我在 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/

相关文章:

ruby - 实现 gem ruby rake 测试的最新技术/最佳实践是什么?

c# - 单元测试 : check if a method is being called

javascript window.open 从回调

ios - 在单独的线程上进行 iPhone CoreLocation 调用的最佳方法?

ios - UIKeyboardWillShowNotification在iOS8中不触发

javascript - phonegap 插件的自动测试

java - 单元测试方法在运行测试时不使用模拟对象。而是调用实际的类和方法

ios - 多个框架和公共(public)库

javascript - 从 Chrome 扩展内容脚本接收数据的 AngularJS 服务有时不会更新 View

ios8 - 仅使用 UIKeyboardWillChangeFrameNotification 通知