我在Swift中编写了我的第一个集成测试.
我正在尝试检查特定网址上是否存在图片.
我想执行头请求并检查响应的状态代码.
我一直收到错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'API violation - multiple calls made to -[XCTestExpectation fulfill].
Run Code Online (Sandbox Code Playgroud)
我试过让期望变弱.
我有以下代码/测试:
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)")
}
})
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Cir*_*ack 14
我建议最好处理多次出现的期望是在完成后将期望变量设置为nil.然后,后续调用将被忽略.
Objective-C的:
// Fulfill and remove. Subsequent messages to nil are ignored.
[multiEx fulfill];
multiEx = nil;`
Run Code Online (Sandbox Code Playgroud)
迅速:
// Fulfill and remove. Optional chaining ends execution on nil.
var multiEx:XCTestExpectation? = expectationWithDescription("multiEx")
...
multiEx?.fulfill()
multiEx = nil
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4096 次 |
| 最近记录: |