异步URLRequest数据任务超时

时间:2016-10-24 15:47:08

标签: swift swift3 nsurlsession xctest xctestexpectation

尝试进行以下测试。它永远不会进入完成块,而是在30秒后超时。这个请求好像从来没有制作过,因为我在查尔斯身上看不到它。

func testAsynchronousURLConnection() {
    let url = URL(string: "https://jsonplaceholder.typicode.com/posts/1")!
    let exp = expectation(description: "Async request")

    var urlRequest = URLRequest(url: url)

    urlRequest.httpMethod = "GET"
    urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")

    let task = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
        XCTAssertNotNil(data, "data should not be nil")
        XCTAssertNil(error, "error should be nil")
        exp.fulfill()
    }

   task.resume()

    waitForExpectations(timeout: 30) { error in
        if let error = error {
            print("Error: \(error)")
        }
    }
}

测试失败并显示错误:

  

异步等待失败:超出预期超时30秒:“异步请求”

我哪里错了?

0 个答案:

没有答案