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

前端 未结 2 1271
感情败类
感情败类 2021-02-11 10:12

If I dispatch_async a block on main queue like this:

-(void) myTask {
  dispatch_async(dispatch_get_main_queue(), ^{
      [self.se         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-02-11 10:25

    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

提交回复
热议问题