簡體   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