ios – 如何为NSNotification编写单元测试

我在 swift工作,我想刷新一个页面,所以我使用通知发送它,我在一个ViewController中发布通知并在另一个添加观察者,它工作正常.我想要做的是在swift中添加单元测试.我查了很多网站但是没能做到.我是新手,不知道从哪里开始.

基本上工作是,当我点击按钮通知被发布时,并且当加载下一个视图控制器时,添加通知观察者.

我该怎么做单元测试

提前致谢

编辑:

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

添加观察者

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

解决方法

一般的解决方案是:使用依赖注入(DI)使您的组件可单元测试.您可以选择使用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
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...