4

我正在进行 POST API 调用。我正在运行 UI 测试并想检查 URL 的响应。但是当我运行这个测试时出现错误。

错误:异步等待失败:超过 10 秒的超时,未实现预期:“消息”

这是我正在调用的测试函数。

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()
}
4

0 回答 0