How to test async method in block using OCMock

人盡茶涼 提交于 2019-12-11 01:45:26

问题


I can't seem to figure out how to test this method:

- (void)writer:(id)writer didFailWithError:(NSError *)error;
{    
 [self.alertView dismissWithClickedButtonIndex:0 animated:YES];

  void (^alertViewBlock)(int) = ^(int buttonIndex)
  {
    if (buttonIndex == 1)
    {
        [self performSelectorOnMainThread:@selector(savePostponeReasonsAsynchronously) withObject:nil waitUntilDone:NO];
    }
    else
    {
        NSLog(@"dismissed");

        self.savePostponeReasonsQueue = nil;
    }
  };

 [self showPostponeReasonFailedAlert:alertViewBlock];
}

Specifically how do I test that the selector savePostponeReasonsAsynchronously was called?

thanks


回答1:


One way to test asynchronous method calls is to wait for them to complete:

__block BOOL isRunning = YES;
[[[myPartialMock expect] andDo:^(NSInvocation *invocation){ isRunning = NO; }] savePostponeReasonsAsynchronously];
myPartialMock writer:nil didFailWithError:nil; 

NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:10];
do {
  [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                           beforeDate:timeout];
} while (isRunning);

STAssertFalse(isRunning, @"Test timed out.");
[myPartialMock verify];

This is a technique I learned by looking at Rob Napier's test code for RNCryptor, which also has some nice tricks using semaphores.




回答2:


I've written a library implementing a "Promise". It turned out that this concept is not only useful for solving complex asynchronous problems, but also for testing:

Suppose, you have some async method and want to check whether a completion handler will be called, and whether it returns the expected result. As a bonus, you also want to set a timeout which the tester waits for a completion handler. If it it expires, the test should fail.

A generic completion hander block:

typedef void (^completion_block_t)(id result);

Your testie, an async method which may be run loop based:

- (void) asyncFooWithCompletion:(completion_block_t)completionHandler;

Your test may look as follows:

- (void) testAsyncFooWithCompletion 
{
    // Your test - executing on the main thread.

    RXPromise* handlerPromise = [RXPromise new];

    [foo asyncFooWithCompletion:^(id result){
        // possibly perform assertions         
        ...

        if (result is expected) {
            // expected in this test scenario
            // resolve promise with a value indicating success:
            [handlerPromise fulfillWithValue:@"OK"];
        }
        else {
            // unexpected in this test scenario
            // resolve promise with an error:
            [handlerPromise rejectWithReason:@"Unexpected result"]; 
        }
    }];

Note: asyncFooWithCompletion:s workload may be scheduled on a run loop, which executes on the corresponding thread - e.g. the main thread.

The completion handler may also be executed on the same run loop, e.g. on the main thread.

    // Set a timeout. If the timeout expires before the handler get called, 
    // the promise will be resolved with a "timeout error":
    [handlerPromise setTimeout:5.0];


    // Register handlers, which get called when _handlerPromise_ will be resolved.
    // We did perform a all assertions in the completion handler. Thus here, 
    // just catch unexpected failures - including a timeout:
    [handlerPromise.thenOn(dispatch_get_main_queue(), nil, 
    ^id(NSError* error) {
        // We are on the main thread
        ADD_FAILURE() << [[error description] UTF8String];
        return error;
     })  
     runLoopWait]; // wait on the current run loop.

     // if we reach here, assertions have been executed (on the main thread) 
     // and all handlers have been returned.

}    // end test


来源:https://stackoverflow.com/questions/18667355/how-to-test-async-method-in-block-using-ocmock

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