gpt4 book ai didi

ios - 异步等待失败的 swift API 请求

转载 作者:搜寻专家 更新时间:2023-11-01 06:20:46 26 4
gpt4 key购买 nike

我已经创建了一个单元测试用例来测试我处理 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com