异步等待失败:超出10秒超时,未达到预期:"消息"

时间:2015-12-08 01:00:32

标签: ios swift nsurlsession xctestexpectation

我正在进行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()
}

0 个答案:

没有答案