14

我有点困惑什么/什么时候做什么expectationForNotification as opposed toexpectationWithDescription`。我一直无法在 swift 中找到任何明确的例子来说明你何时以及如何处理这个电话。

我假设它可能是为了测试通知,但看起来它可能只是addObserver()通知中心整个调用的更方便的包装器。

有人可以简要解释一下它的作用,何时使用它,也许还有几行示例代码?

4

3 回答 3

30

正如您已经想象的那样, expectationForNotification是一种方便的期望,用于检查是否提出了通知。

本次测试:

func testItShouldRaiseAPassNotificationV1() {
    let expectation = expectationWithDescription("Notification Raised")
    let sub = NSNotificationCenter.defaultCenter().addObserverForName("evPassed", object: nil, queue: nil) { (not) -> Void in
        expectation.fulfill()
    }
    NSNotificationCenter.defaultCenter().postNotificationName("evPassed", object: nil)
    waitForExpectationsWithTimeout(0.1, handler: nil)
    NSNotificationCenter.defaultCenter().removeObserver(sub)
}

可以用这个代替:

func testItShouldRaiseAPassNotificationV2() {
    expectationForNotification("evPassed", object: nil, handler: nil)
    NSNotificationCenter.defaultCenter().postNotificationName("evPassed", object: nil)
    waitForExpectationsWithTimeout(0.1, handler: nil)
}

你可以在这个Objc.io 号中找到一个很好的解释。

于 2015-04-22T16:09:55.610 回答
4

为了理解 和 之间的区别expectation(forNotification:, object:, handler:)expectation(description:)XCTestCase用 Swift 3 构建了一个简单的子类。

在这里,我们要测试BlockOperation发布 a 的a 是否使用请求的值 50Notification更新了我们类的指定Int?属性。


1.expectation(description:)搭配使用addObserver(_:, selector:, name:, object:)

import XCTest

class AppTests: XCTestCase {

    var testExpectation: XCTestExpectation?
    var finalAmount: Int?

    func testFinalAmount() {
        let notificationName = Notification.Name(rawValue: "BlockNotification")

        // Set self as an observer
        let selector = #selector(updateFrom(notification:))
        NotificationCenter.default.addObserver(self, selector: selector, name: notificationName, object: nil)

        // Set expectation
        testExpectation = expectation(description: "Did finish operation expectation")

        // Set and launch operation block and wait for expectations
        let operation = BlockOperation(block: {
            NotificationCenter.default.post(name: notificationName, object: nil, userInfo: ["amount": 50])
        })
        operation.start()
        waitForExpectations(timeout: 3, handler: nil)

        // Asserts
        XCTAssertNotNil(finalAmount)
        XCTAssertEqual(finalAmount, 50)
    }

    func updateFrom(notification: Notification) {
        if let amount = notification.userInfo?["amount"] as? Int {
            self.finalAmount = amount
        }
        self.testExpectation?.fulfill()
    }

}

2.expectation(description:)配合使用addObserver(forName:, object:, queue:, using:)

import XCTest

class AppTests: XCTestCase {

    var finalAmount: Int?

    func testFinalAmount() {
        let notificationName = Notification.Name(rawValue: "BlockNotification")

        // Set expectation
        let testExpectation = expectation(description: "Did finish operation expectation")

        // Set self as an observer
        let handler = { (notification: Notification) -> Void in
            if let amount = notification.userInfo?["amount"] as? Int {
                self.finalAmount = amount
            }
            testExpectation.fulfill()
        }
        NotificationCenter.default.addObserver(forName: notificationName, object: nil, queue: nil, using: handler)

        // Set and launch operation block and wait for expectations
        let operation = BlockOperation(block: {
            NotificationCenter.default.post(name: notificationName, object: nil, userInfo: ["amount": 50])
        })
        operation.start()
        waitForExpectations(timeout: 3, handler: nil)

        // Asserts
        XCTAssertNotNil(finalAmount)
        XCTAssertEqual(finalAmount, 50)
    }

}

3.使用expectation(forNotification:, object:, handler:)

import XCTest

class AppTests: XCTestCase {

    var finalAmount: Int?

    func testFinalAmount() {
        let notificationName = Notification.Name(rawValue: "BlockNotification")

        // Set expectation
        let handler = { (notification: Notification) -> Bool in
            if let amount = notification.userInfo?["amount"] as? Int {
                self.finalAmount = amount
            }
            return true
        }
        expectation(forNotification: notificationName.rawValue, object: nil, handler: handler)

        // Set and launch operation block and wait for expectations
        let operation = BlockOperation(block: {
            NotificationCenter.default.post(name: notificationName, object: nil, userInfo: ["amount": 50])
        })
        operation.start()
        waitForExpectations(timeout: 3, handler: nil)

        // Asserts
        XCTAssertNotNil(finalAmount)
        XCTAssertEqual(finalAmount, 50)
    }

}

tl;博士

在我们的测试用例中使用expectation(forNotification: String, object:, handler:)代替提供了一些优点:expectation(description:)

  • 我们的测试现在需要更少的代码行(31 行而不是 35 或 37 行),
  • 我们的测试不再需要addObserver(_:, selector:, name:, object:)#selectoror一起使用addObserver(forName:, object:, queue:, using:)
  • 我们的测试不再需要将XCTestExpectation实例声明为类的属性或测试方法的作用域变量,并将其标记为在某个点上遇到了fulfill().
于 2016-08-24T16:14:26.360 回答
2

你不应该依赖 UIKit 的 NotificationCenter。为您的类型设置一个边界并仅在您的类型将命令发送到正确的对象时进行测试。这是一个如何使 NotificationCenter 采用您的代码的示例。(我现在无法访问 Xcode,所以可能有一些错字)

protocol NotificationCenterProtocol {
  func post(notification: Notification)
}

extension NotificationCenter: NotificationCenterProtocol {}

class SpyNotificationCenter: NotificationCenterProtocol {
  var didPostNotification = false

  func post(notification: Notification) {
    didPostNotification = true
  }

}
于 2017-05-25T20:11:52.390 回答