异步等待失败swift API请求



我创建了一个单元测试用例来测试我处理API请求的函数。

Bellow我提到了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)
}

当我运行这个测试用例时,它会给出一个错误

异步等待失败:超过了30秒的超时,预期值未实现:"异步请求"。

它如何显示带有错误的响应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()

最新更新