如何使用OCMock在块中测试异步方法

编程入门 行业动态 更新时间:2024-10-25 20:22:26
本文介绍了如何使用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在块中测试异步方法

本文发布于:2023-11-03 16:33:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   测试   方法   OCMock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!