我的测试用例永远不会结束

时间:2015-09-02 21:26:18

标签: ios unit-testing asynchronous

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

这是我的测试:

-(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

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

1 个答案:

答案 0 :(得分:0)

您在调用division_records = Record.where(division: division).as_json date_range_records = division_records.select { |r| ((start_date..end_date).include? Date.strptime(r["date"], "%m/%d/%y")) }后正在记录您的块可能会在日志语句之前返回,即使它已完成也会挂起。

交换线条的顺序,确保你不会完成直到最后。