我已经创建了一个单元测试用例来测试我处理 API 请求的功能。
下面我提到了XC测试用例代码
var expectation:XCTestExpectation?
func testRequestFunction () {
expectation = self.expectationWithDescription("asynchronous request")
let test = HVRequest.init(subdomain: "staging", token: "jijfio88fhu0387fh", type: .GET, command: "estm") {
(success) -> Void in
}
test.request()
self.waitForExpectationsWithTimeout(30, handler: nil)
}
当我运行这个测试用例时,它给出了一个错误
Asynchronous wait failed: Exceeded timeout of 30 seconds, with unfulfilled expectations: "asynchronous request".
它如何显示带有错误的响应 JSON 对象。
请帮我解决问题
最佳答案
XCTestExpectation
正在等待。当你得到响应时,你必须fulfill
(意思是:说出我得到响应的expectation
,继续)。
let test = HVRequest.init(subdomain: "staging", token: "jijfio88fhu0387fh", type: .GET, command: "estm") {
(success) -> Void in
expectation.fulfill() // add this line
}
test.request()
关于ios - 异步等待失败的 swift API 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34690893/