In unit test, execute the block passed in queue with dispatch_asyc

[亡魂溺海] 提交于 2019-12-03 10:13:38

You could create the queue in your test function.

-(void) myTask2:(dispatch_queue_t*)queue {
    dispatch_async(*queue, ^{
        [self.service fetchData];
    });
}

-(void)testMyTask2{
    dispatch_queue_t queue = dispatch_queue_create("my.sequential.queue", NULL);
    [myObj myTask2:&queue];

    dispatch_sync(queue, ^{
    });
}

(Just realised currentRunLoop is not needed)

For execute test in async block use XCTestExpectation class

-(void) myTask2 {
  XCTestExpectation *expectation = [self expectationWithDescription:@"catch is called"];
  dispatch_async(dispatch_queue_create("my.sequetial.queue", NULL), ^{
      [self.serviceClient fetchDataForUserId:self.userId];
      [expectation fulfill];
   });

   [self waitForExpectationsWithTimeout:Timeout handler:^(NSError *error) {
        //check that your NSError nil or not
    }];
}

Hope this help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!