我正在进行POST API调用。我正在运行UI测试,并希望检查URL的响应。但是,当我运行此测试时,我收到了一个错误。
错误:异步等待失败:超出10秒的超时,未满足期望:"消息"
这是我正在调用的Test函数。
func testgetdata() {
let expectation = expectationWithDescription("asynchronous call waitng ")
let test:mainclass = mainclass()
test.getData(){ (response,data) in
if let HTTPResponse = response as? NSHTTPURLResponse, MIMEType = HTTPResponse.MIMEType {
XCTAssertEqual(HTTPResponse.statusCode, 200, "HTTP response status code should be 200")
XCTAssertEqual(MIMEType, "application/json", "HTTP response content type should be text/html")
} else {
XCTFail("Response was not NSHTTPURLResponse")
}
expectation.fulfill()
}
waitForExpectationsWithTimeout(10.0, handler: nil)
}
主要功能:
func getData(callback: (response:NSURLResponse, data:NSData) -> ()) {
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let requestURL = NSMutableURLRequest(URL: NSURL(string: "URL")!)
requestURL.HTTPMethod = "POST"
let task = session.dataTaskWithRequest(requestURL) { (data, response, error) in
print(response)
callback(response: response!,data: data!)
}
task.resume()
}