繁体   English   中英

如何为 NSNotification 编写单元测试

[英]How to write unit test for NSNotification

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

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

我如何进行单元测试

提前致谢

编辑:代码

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

并将观察者添加为

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

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实例的便捷方法,但为了更多控制,您可以实例化一个并自己配置它。 请参阅文档

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

这是一个更简单的解决方案:

第 1 步:在环境变量中捕获 notificationCenter 对象,以便能够在您的单元测试中用一些间谍类替换它。

// In your production code:
var notificationCenter = NSNotificationCenter.defaultCenter()

// The code you are testing:
notificationCenter.postNotificationName("notificationName", object: nil)

第 2 步:使用继承定义您的间谍类,以便能够检测通知是否已发布。

// In your test code
private class NotificationCenterSpy: NotificationCenter {
    var notificationName: String?

    override func post(_ notificationName: String, object anObject: Any?) 
    {
        self.notificationName = aName
    }
}

第 3 步:替换单元测试中的环境变量。

// In your test code:

// Given
// setup SUT as usual ...
let notificationCenterSpy = NotificationCenterSpy()
sut.notificationCenter = notificationCenterSpy

// When
sut.loadView()

// Then
XCTAssertEqual(notificationCenterSpy.notificationName, "notificationName")

第 4 步:测试接收器视图控制器

您不应该测试接收者视图控制器是否观察到变化,您应该测试行为。

收到通知时应该发生什么事情? 这就是您应该测试的内容,从您的测试代码中发布通知并查看是否发生了这种行为(在您的情况下是否刷新了页面)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM