如何编写NSNotification的unit testing

我在swift中工作,我想刷新一个页面,所以我使用通知发送它,我在一个ViewController发布通知,并添加观察者在另一个,它是完美的工作。 我想要做的就是快速添加unit testing。 我查了很多网站,但无法做到这一点。 我是新来的迅速,不知道从哪里开始。

基本上工作是,当我点击button通知张贴和当下一个视图控制器加载通知观察员被添加。

我怎么做unit testing

提前致谢

编辑:代码

NSNotificationCenter.defaultCenter().postNotificationName("notificationName", object: nil) 

并添加观察者

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "vvv:",name:"notificationName", object: nil) 

一般的解决scheme是:使用dependency injection(DI)使您的组件可以unit testing。 你可以select使用DI框架(我不知道是否有任何好的Swift框架存在)或使用本地方法(即传递对象周围)

你的问题的一个可能的方法是包装NSNotificationCenter使其可嘲弄/注射。

这只是一个基本的想法,你可以如何分离依赖关系。 请不要复制和粘贴下面的代码,并期望它的工作不理解它。

 import Foundation protocol 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($0) } } } 

 // production code // in AppDeletate.applicationDidFinishLaunching let notificationCenter = MyNotificationCenter(NSNotificationCenter.defaultCenter()) // pass it to your root view controller let rootViewController = RootViewController(notificationCenter: notificationCenter) // or rootViewController.notificationCenter = notificationCenter // in controller viewDidLoad self.myView.notificationCenter = self.notificationCenter // when you need to create controller // pass notificationCenter to it let controller = MyController(notificationCenter: notificationCenter) // in unit test func 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 }