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

标签 ios swift asynchronous xctest

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

相关文章:

ios - 是否在运行时生成图像?

javascript - 回调作为对象 - 它与 JavaScript 中的异步编程有何关系

ios - 为什么 UITableView 没有显示在 UIViewController 上?

ios - 如何在 UICollectionViewCell 中缩放 UIScrollView?

ios - 在 NavigationLink 中设置 editMode 时,SwiftUI EditButton() 不起作用

ios - 为什么不调用 MKMapView 中的 clusterAnnotationForMemberAnnotations?

javascript - 检测异步脚本的 onload/onerror

javascript - 异步函数声明在 Internet Explorer 中需要 ';'

ios - Swift 中的惰性属性等同于 Objective C 中的惰性 Init getter

ios - 在 UIPageViewController 中获取 ViewController 的容器 View