基本上工作是,当我点击按钮通知被发布时,并且当加载下一个视图控制器时,添加通知观察者.
我该怎么做单元测试
提前致谢
编辑:
码
NSNotificationCenter.defaultCenter().postNotificationname("notificationname",object: nil)
并添加观察者
NSNotificationCenter.defaultCenter().addobserver(self,selector: "vvv:",name:"notificationname",object: nil)解决方法 一般的解决方案是:使用依赖注入(DI)使您的组件可单元测试.您可以选择使用DI框架(我不知道Swift是否存在任何良好的框架)或使用本机方法(即传递对象)
解决问题的一种可能方法是包装NSNotificationCenter以使其可模拟/可注入.
这只是一个基本的想法,你可以如何解耦依赖.请不要只复制&粘贴下面的代码并期望它在不理解它的情况下工作.
import Foundationprotocol NotificationCenter { func postNotificationname(name: String,object: AnyObject?) // you can make it take the arguments as NSNotificationCenter.addobserver func addobserver(callback: AnyObject? -> VoID)}class MyNotificationCenter : NotificationCenter { var _notificationCenter: NSNotificationCenter init(_ center: NSNotificationCenter) { _notificationCenter = center } func postNotificationname(name: String,object: AnyObject?) { // call NSNotificationCenter.postNotificationname } func addobserver(callback: AnyObject? -> VoID) { // call NSNotificationCenter.addobserver }}class MockNotificationCenter : NotificationCenter { var postednotifications: [(String,AnyObject?)] = [] var observers: [AnyObject? -> VoID] = [] func postNotificationname(name: String,object: AnyObject?) { postednotifications.append((name,object)) } func addobserver(callback: AnyObject? -> VoID) { observers.append(callback) }}class MyVIEw { var notificationCenter: NotificationCenter init(notificationCenter: NotificationCenter) { self.notificationCenter = notificationCenter } func handleAction() { self.notificationCenter.postNotificationname("name",object: nil) }}class MyController { var notificationCenter: NotificationCenter init(notificationCenter: NotificationCenter) { self.notificationCenter = notificationCenter } func vIEwDIDLoad() { self.notificationCenter.addobserver { println(总结// production code// in AppDeletate.applicationDIDFinishLaunchinglet notificationCenter = MyNotificationCenter(NSNotificationCenter.defaultCenter())// pass it to your root vIEw controllerlet rootVIEwController = RootVIEwController(notificationCenter: notificationCenter)// orrootVIEwController.notificationCenter = notificationCenter// in controller vIEwDIDLoadself.myVIEw.notificationCenter = self.notificationCenter// when you need to create controller// pass notificationCenter to itlet controller = MyController(notificationCenter: notificationCenter)// in unit testfunc testMyVIEw() { let notificationCenter = MockNotificationCenter() let myVIEw = MyVIEw(notificationCenter: notificationCenter) // do something with myVIEw,assert correct notification is posted // by checking notificationCenter.postednotifications}func testMyController() { let notificationCenter = MockNotificationCenter() let myController = MyController(notificationCenter: notificationCenter) // assert notificationCenter.observers is not empty // call it and assert correct action is performed}) } }}
以上是内存溢出为你收集整理的ios – 如何为NSNotification编写单元测试全部内容,希望文章能够帮你解决ios – 如何为NSNotification编写单元测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)