如何编写单元测试以接收nsnotification异步? [英] How write unit test for receiving nsnotification asynchronous?

查看:108
本文介绍了如何编写单元测试以接收nsnotification异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用完成处理程序调用其余的Web服务,如果成功,我将发送NSNotification.

I call rest web servic with completion handler and if succeed I send NSNotification.

问题在于如何编写单元测试以断言在成功的情况下发送通知.

The problem is how to write unit test to assert that the notification is sent in case of success.

任何帮助将不胜感激.

推荐答案

您可以为通知添加期望:

You can add an expectation for the notification:

expectationForNotification("BlaBlaNotification", object: nil) { (notification) -> Bool in

// call the method that fetches the data
sut.fetchData()  

waitForExpectationsWithTimeout(5, handler: nil)

但是我个人将这分为两个测试.一个用于获取数据(使用存根进行了测试),另一个用于发送通知.

But personally I would split this is two tests. One for the fetching of the data (tested using a stub) and one for the sending of the notification.

这篇关于如何编写单元测试以接收nsnotification异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆