我有点困惑什么/什么时候做什么expectationForNotification as opposed to
expectationWithDescription`。我一直无法在 swift 中找到任何明确的例子来说明你何时以及如何处理这个电话。
我假设它可能是为了测试通知,但看起来它可能只是addObserver()
通知中心整个调用的更方便的包装器。
有人可以简要解释一下它的作用,何时使用它,也许还有几行示例代码?
我有点困惑什么/什么时候做什么expectationForNotification as opposed to
expectationWithDescription`。我一直无法在 swift 中找到任何明确的例子来说明你何时以及如何处理这个电话。
我假设它可能是为了测试通知,但看起来它可能只是addObserver()
通知中心整个调用的更方便的包装器。
有人可以简要解释一下它的作用,何时使用它,也许还有几行示例代码?
正如您已经想象的那样, 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 号中找到一个很好的解释。
为了理解 和 之间的区别expectation(forNotification:, object:, handler:)
,expectation(description:)
我XCTestCase
用 Swift 3 构建了一个简单的子类。
在这里,我们要测试BlockOperation
发布 a 的a 是否使用请求的值 50Notification
更新了我们类的指定Int?
属性。
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()
}
}
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)
}
}
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)
}
}
在我们的测试用例中使用expectation(forNotification: String, object:, handler:)
代替提供了一些优点:expectation(description:)
addObserver(_:, selector:, name:, object:)
与#selector
or一起使用addObserver(forName:, object:, queue:, using:)
,XCTestExpectation
实例声明为类的属性或测试方法的作用域变量,并将其标记为在某个点上遇到了fulfill()
.你不应该依赖 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
}
}