热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何编写NSNotification的单元测试

如何解决《如何编写NSNotification的单元测试》经验,为你挑选了2个好方法。

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

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

我该怎么做单元测试

提前致谢

编辑:代码

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

并添加观察者

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

zpasternack.. 16

XCTest有一个专门用于测试通知的类:XCTNSNotificationExpectation.您可以创建其中一个期望,并在收到通知时完成.你会像以下一样使用它:

// MyClass.swift
extension Notification.Name {
    static var MyNotification = Notification.Name("com.MyCompany.MyApp.MyNotification")
}

class MyClass {
    func sendNotification() {
        NotificationCenter.default.post(name: .MyNotification,
                                      object: self,
                                    userInfo: nil)
    }
}


// MyClassTests.swift
class MyClassTests: XCTestCase {
    let classUnderTest = MyClass()

    func testNotification() {
        let notificatiOnExpectation= expectation(forNotification: .MyNotification, 
                                                           object: classUnderTest, 
                                                          handler: nil)

        classUnderTest.sendNotification()

        waitForExpectations(timeout: 5, handler: nil)
    }
}

XCTestCaseexpectation(forNotification:object:handler:)是一种创建实例的便捷方法XCTNSNotificationExpectation,但为了更多控制,您可以实例化一个并自己配置它.查看文档.



1> zpasternack..:

XCTest有一个专门用于测试通知的类:XCTNSNotificationExpectation.您可以创建其中一个期望,并在收到通知时完成.你会像以下一样使用它:

// MyClass.swift
extension Notification.Name {
    static var MyNotification = Notification.Name("com.MyCompany.MyApp.MyNotification")
}

class MyClass {
    func sendNotification() {
        NotificationCenter.default.post(name: .MyNotification,
                                      object: self,
                                    userInfo: nil)
    }
}


// MyClassTests.swift
class MyClassTests: XCTestCase {
    let classUnderTest = MyClass()

    func testNotification() {
        let notificatiOnExpectation= expectation(forNotification: .MyNotification, 
                                                           object: classUnderTest, 
                                                          handler: nil)

        classUnderTest.sendNotification()

        waitForExpectations(timeout: 5, handler: nil)
    }
}

XCTestCaseexpectation(forNotification:object:handler:)是一种创建实例的便捷方法XCTNSNotificationExpectation,但为了更多控制,您可以实例化一个并自己配置它.查看文档.



2> Bryan Chen..:

一般的解决方案是:使用依赖注入(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
}


@Honey你是对的,该方法应该具有名称和对象参数。“ MockNotificationCenter”应仅以单元测试代码编写。
推荐阅读
author-avatar
贴进你的心聆听你的世界
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有