【问题标题】:How to write unit test for NSNotification如何为 NSNotification 编写单元测试
【发布时间】:2015-02-03 06:27:14
【问题描述】:

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

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

如何进行单元测试

提前致谢

编辑: 代码

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

并将观察者添加为

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

【问题讨论】:

  • 不要NSNotification编写单元测试。你用一个类包装它,然后它是你的组件的一个可模拟/可注入的依赖项。
  • 你能展示一些你的代码吗?你是如何使用 NSNotification 的?

标签: ios unit-testing swift ios8 nsnotificationcenter


【解决方案1】:

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 实例的便捷方法,但为了获得更多控制,您可以实例化一个实例并自行配置。见the docs

【讨论】:

  • 一个澄清是,在expectation 中,object 通常不是“被测类”(即不是发送通知的类),而是在通知中发送的对象。在这个特定的例子中,它们是相同的,但通常它们不是。例如,在 OP 示例中,它是 nil: expectation(forNotification: .MyNotification, object: nil, handler: nil)
【解决方案2】:

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

【讨论】:

  • 我是 swift 新手,你能告诉我代码工作背后的基本思想是什么吗?
  • @user2413621 google 并学习依赖注入。这是一个通用的编程概念,与 Swift 无关。
  • 我收到一个错误 Type 'MyNotificationCenter' does not conform to protocol 'NotificationCenter' in line let notificationCenter = MyNotificationCenter(NSNotificationCenter.defaultCenter()) // 别处让 myView = MyView(notificationCenter: notificationCenter )
  • @user2413621 你可以检查数组的计数
  • @Honey 你是对的,方法应该有名称和对象参数。 MockNotificationCenter 只能用单元测试代码编写。
【解决方案3】:

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

第 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 步:测试接收器视图控制器

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

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

【讨论】:

    猜你喜欢
    • 2016-03-26
    • 2018-01-17
    • 2012-01-06
    • 2019-03-17
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多