API 违规 - 多次调用 -[XCTestExpectation meet] [英] API violation - multiple calls made to -[XCTestExpectation fulfill]
本文介绍了API 违规 - 多次调用 -[XCTestExpectation meet]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用 Swift 编写我的第一个集成测试.
I'm writing one of my first integration tests in Swift.
我正在尝试检查特定网址中是否存在图像.
I'm trying to check if an image exists at a particular url.
我想执行一个头部请求并检查响应的状态代码.
I want to perform a head request and check the response's status code.
我不断收到错误消息:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'API violation - multiple calls made to -[XCTestExpectation fulfill].
我已尝试将期望设为弱变量.
I've tried making the expectation a weak variable.
我有以下代码/测试:
func testAndroidImagesExist() {
weak var expectation: XCTestExpectation?
expectation = expectationForNotification(kBaoNotification_ManifestImportCompleted, object: nil) { (notification: NSNotification!) -> Bool in
let userInfo: NSDictionary = notification.userInfo!
var titles = userInfo.valueForKey("titles") as? NSArray
titles?.enumerateObjectsUsingBlock({ (t: AnyObject!, idx: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
let title = t as NSDictionary
let titleLabel = title.valueForKey("title") as String
let parameters = title.valueForKey("parameters") as NSDictionary
let androidImageUrl = parameters.valueForKey("android_logo_url") as String
var androidRequest = NSMutableURLRequest(URL: NSURL(string: androidImageUrl)!)
androidRequest.HTTPMethod = "HEAD"
var androidResponse: NSURLResponse?
var androidData = NSURLConnection.sendSynchronousRequest(androidRequest, returningResponse: &androidResponse, error: nil)
var androidHttpResponse = androidResponse as? NSHTTPURLResponse
if androidHttpResponse != nil {
if androidHttpResponse!.statusCode == 404 {
XCTFail("Android image not found for title \(titleLabel)")
}
} else {
XCTFail("No response from android image for title \(titleLabel)")
}
})
expectation?.fulfill()
return true
}
waitForExpectationsWithTimeout(10, handler: { (error: NSError!) -> Void in
if (error != nil) {
XCTFail("Timeout error: \(error)")
}
})
}
有什么想法吗?
推荐答案
我建议最好处理多次发生的期望是在满足后将期望变量设置为 nil.然后,后续调用将被忽略.
I suggest best handing of a multiple occurring expectation is to set your expectation variable to nil after the fulfill. Then, subsequent calls will be ignored.
目标 C:
// Fulfill and remove. Subsequent messages to nil are ignored.
[multiEx fulfill];
multiEx = nil;`
斯威夫特:
// Fulfill and remove. Optional chaining ends execution on nil.
var multiEx:XCTestExpectation? = expectationWithDescription("multiEx")
...
multiEx?.fulfill()
multiEx = nil
这篇关于API 违规 - 多次调用 -[XCTestExpectation meet]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文