本文介绍了如何使用OCMock在块中测试异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎无法弄清楚如何测试此方法:
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]; }具体来说,如何测试选择器savePostponeReasonsAsynchronously被调用了?
Specifically how do I test that the selector savePostponeReasonsAsynchronously was called?
谢谢
推荐答案测试异步方法调用的一种方法是等待它们完成:
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];这是我通过查看Rob Napier的测试代码所学的技术RNCryptor ,使用信号量也有一些不错的技巧.
This is a technique I learned by looking at Rob Napier's test code for RNCryptor, which also has some nice tricks using semaphores.
更多推荐
如何使用OCMock在块中测试异步方法
发布评论