我有点困惑什么/什么时候做expectationForNotification as opposed to
期望与描述`。我无法在 swift 中找到任何明确的示例来说明您何时以及如何处理此调用。
我假设它可能是为了测试通知,但看起来它可能只是整个过程的一个更方便的包装器addObserver()
通知中心的呼叫。
有人可以简要解释一下它的作用、何时使用它,也许还有几行示例代码?
正如你已经想象的那样通知期望是检查是否发出通知的方便期望。
本次测试:
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 编号 http://www.objc.io/issue-15/xctest.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)