XCTestExpectation出错:API违规 - 多次调用 - [XCTestExpectation fulfill] [英] Error with XCTestExpectation: API violation - multiple calls made to -[XCTestExpectation fulfill]

查看:1058
本文介绍了XCTestExpectation出错:API违规 - 多次调用 - [XCTestExpectation fulfill]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode 6(Beta 5)中使用XCTestExpectations进行异步测试。我每次运行时都会单独传递所有异步测试。但是,当我尝试运行我的整个套件时,一些测试没有通过,应用程序崩溃了。

I'm using XCTestExpectations in Xcode 6 (Beta 5) for asynchronous testing. All my asynchronous tests pass individually every time I run them. However, when I try to run my entire suite, some tests do not pass, and the app crashes.

我得到的错误是 API违规 - 多次调用 - [XCTestExpectation fulfill] 。实际上,在单一方法中并非如此;我测试的一般格式如下所示:

The error I get is says API violation - multiple calls made to -[XCTestExpectation fulfill]. Indeed, this is not true within a single method; my general format for my tests is shown below:

- (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 */
    }];
}

同样,这些测试在单独运行时确实通过,并且它们实际上正在制作网络请求(完全按照预期工作),但是一起,测试集合无法运行。

Again, these tests do pass when run individually, and they are actually making network requests (working exactly as intended), but together, the collection of tests fail to run.

我能够看一下这篇文章这里,但无法让解决方案为我工作。

I was able to have a look at this post here, but was unable to get the solution to work for me.

此外,我正在运行OSX Mavericks并使用Xcode 6(Beta 5)。

Additionally, I'm running OSX Mavericks and using Xcode 6 (Beta 5).

推荐答案

我不认为使用 __弱 __ block 是一个很好的方法。我已经使用 XCTestExpectation 写了很多单元测试一段时间,直到现在才遇到这个问题。我终于发现问题的真正原因可能导致我的应用程序中的错误。我的问题的根本原因是 startAsynchronousTaskWithDuration 多次调用completionHandler。修复后,API违规消失了!

I don't think using __weak or __block is a good approach. I have written many unit tests using XCTestExpectation for awhile and never had this problem until now. I finally found out that real cause of the problem which potentially may cause bugs in my app. The root cause of my problem is that startAsynchronousTaskWithDuration calls the completionHandler multiple time. After I fix it the API violation went away!

[self startAsynchronousTaskWithDuration:4 completionHandler:^(id result, NSError *error) {
    XCTAssertNotNil(result);
    XCTAssertNil(error);
    [expectation fulfill];
}];

虽然我花了几个小时来修复我的单元测试,但我开始意识到API违规错误这将帮助我避免我的应用程序中的未来运行时问题。

Although it took me a few hours to fix my unit tests but I came to appreciate the API violation error which will help me avoid future runtime problem in my app.

这篇关于XCTestExpectation出错:API违规 - 多次调用 - [XCTestExpectation fulfill]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆