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

前端之家收集整理的这篇文章主要介绍了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
}
原文链接:https://www.f2er.com/iOS/331745.html

猜你在找的iOS相关文章