【问题标题】:My Test case never ends我的测试用例永无止境
【发布时间】:2015-09-02 21:26:18
【问题描述】:

我正在尝试测试对服务器的异步请求。 我发送请求,得到响应,但是测试挂起并且没有结束

这是我的测试:

-(void)testClearAppsCache{
    XCTestExpectation *expectation = [self expectationWithDescription:@"callServicesWithParameters"];
    [self.apiClient callServicesWithParameters:nil//@{@"version":@""}
                                       success:^(BridgeModel *bridge) {
                                           BOOL onMainThread = [NSThread isMainThread];
                                           XCTAssertFalse(onMainThread,@"should be on main thread");
                                           XCTAssertNotNil(bridge);
                                           [expectation fulfill];
                                           NSLog(@"done");
                                       }

                                       failure: ^(NSError * error) {
                                           [expectation fulfill];
                                           LogInfo(@"error gettings services:%@",error);
                                       }];

    [self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
        NSLog(@"waiting for expectation");
        if(error){
            NSLog(@"error:%@",error);
            XCTFail(@"error:%@",error);
        }else {
            XCTAssertNil(error,@"test finished");
        }
    }];

    NSLog(@"done testing");

}

这是测试后打印的日志:

2015-09-03 00:16:00.758 MyApp[1960:35258312] I,-[AppDelegate application:didFinishLaunchingWithOptions:]:55 application:didFinishLaunchingWithOptions:
2015-09-03 00:16:00.851 MyApp[1960:35258312] Request http://api.something.json?
Test Suite 'Selected tests' started at 2015-09-02 21:16:01 +0000
Test Suite 'MyApp Tests.xctest' started at 2015-09-02 21:16:01 +0000
Test Suite 'ManagerTests' started at 2015-09-02 21:16:01 +0000

2015-09-03 00:16:01.151 MyApp[1960:35258312] I,-[apiClient sendRequest:]:515
sending GET request to path:http:...
2015-09-03 00:16:01.152 MyApp[1960:35258312] D,-[apiClient sendRequest:]:516

2015-09-03 00:16:01.152 MyApp[1960:35258312] D,-[apiClient asyncRequestDataForServerRequest:]:161 added '?'
2015-09-03 00:16:01.152 MyApp[1960:35258312] D,-[apiClient asyncRequestDataForServerRequest:]:165 path with params:http://api.something.json?
2015-09-03 00:16:01.152 MyApp[1960:35258312] D,-[apiClient startRequest:]:183 started connection
2015-09-03 00:16:01.902 MyApp[1960:35258364] D,-[apiClient connection:didReceiveResponse:]:249 Response recieved from url:http://api.something.json?
2015-09-03 00:16:01.902 MyApp[1960:35258348] D,-[apiClient connectionDidFinishLoading:]:267 Data recieved:
2015-09-03 00:16:01.904 MyApp[1960:35258348] D,-[apiClient connectionDidFinishLoading:]:268 statusCode:200
2015-09-03 00:16:03.940 MyApp[1960:35258348] D,-[apiClient connectionDidFinishLoading:]:277 onMainThread?0
2015-09-03 00:16:05.189 MyApp[1960:35258348] done

请求运行并成功,收到正确的响应和状态码,成功块运行,但是在成功块完成后,没有任何反应,测试挂起 - 我该如何解决?

【问题讨论】:

    标签: ios unit-testing asynchronous


    【解决方案1】:

    您在调用 [expectation fulfill]; 之后正在记录您的块可能会在 log 语句之前返回,看起来挂起,即使它确实完成了。

    交换行的顺序,确保你直到最后才完成。

    【讨论】:

    • 没有帮助,测试仍然挂起
    • 我解决了这个问题,我遇到了 stackoverflow 问题,但系统由于某种原因没有报告它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 2016-03-20
    相关资源
    最近更新 更多