如何编写NSNotification的单元测试 [英] How to write unit test for NSNotification

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

问题描述

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

I am working in swift, I want to refresh a page so I am sending it using notification, I am posting a notification in one ViewController and adding observer in another and it is working perfectly. What I want to do is add unit test to it in swift. I checked many sites but was not able to do it. I am new to swift and don't know where to start.

基本上工作是,当我点击按钮通知发布时以及加载下一个视图控制器时通知观察员已添加。

Basically the working is, when i click the button notification is posted and when the next view controller is loaded the notification observer is added.

我如何进行单元测试

提前致谢

编辑:
代码

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

并将观察者添加为

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


推荐答案

一般解决方案是:使用依赖注入(DI)使您的组件可单元测试。您可以选择使用DI框架(我不知道Swift是否存在任何良好的框架)或使用本机方法(即传递对象)

The general solution is: Use dependency injection (DI) to make your components unit-testable. You can choose use a DI framework (I don't know if there is any good framework for Swift exists yet) or use native approach (i.e. pass object around)

一个你的问题的可能方法是包装 NSNotificationCenter 使其成为可模拟/可注射的。

One possible approach for your problem is to wrap NSNotificationCenter to make it mockable/injectable.

这只是一个基本的想法如何解耦依赖关系。请不要只复制&粘贴下面的代码并期望它在不理解的情况下工作。

This is just a basic idea how you can decouple dependencies. Please don't just copy & paste the code below and expect it to work without understanding it.

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
}

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

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