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

前端 未结 2 1266
感情败类
感情败类 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

    0 讨论(0)
  • 2021-02-11 10:35

    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)

    0 讨论(0)
提交回复
热议问题