jeudi 23 juin 2016

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

If I dispatch_async a block on main queue like this:

-(void) myTask {
  dispatch_async(dispatch_get_main_queue(), ^{
      [self.service fetchData];
   });
}

In unit test, I can execute the block passed in main queue by manually run the main loop like this:

-(void)testMyTask{
  // call function under test
  [myObj myTask];
  // run the main loop manually!
  [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
  // now I can verify the function 'fetchData' in block is called
  ...
}

Now, I have another similar function which dispatch block to an sequential queue other than main queue:

-(void) myTask2 {
  dispatch_async(dispatch_queue_create("my.sequetial.queue", NULL), ^{
      [self.serviceClient fetchDataForUserId:self.userId];
   });
}

In unit test, how can I execute the block manually now?

Aucun commentaire:

Enregistrer un commentaire