Swift - 异步等待失败:超过20秒的超时,没有实现期望:" myExpectation"

时间:2018-03-15 10:44:36

标签: swift unit-testing

以下是测试功能的代码:

   func testGetDataFromURL()  {

    var exp = self.expectation(description: "myExpectation")
    exp.expectedFulfillmentCount = 3
    self.objImpleTest.letsCheckReqAndRes()
    exp.fulfill()

   waitForExpectations(timeout: 20) { (error) in
        XCTAssertNil(error, "Test timed out. \ 
 (error?.localizedDescription)")

        print("X: async expectation")
    }
}

这是主要功能的代码:

func letsCheckReqAndRes() {
    var gotResults: String = ""

    //var results: String = ""

    let headers = [
        "Cache-Control": "no-cache",
        "Postman-Token": "0d2eb1be-fb26-4425-b6b1-f1fa37005cee"
    ]

    let request = NSMutableURLRequest(url: NSURL(string: 
  "http://www.mocky.io/v2/5a964d253200006e005e2d50")! as URL,
                                      cachePolicy: 
 .useProtocolCachePolicy,
                                      timeoutInterval: 10.0)
    request.httpMethod = "GET"

    request.allHTTPHeaderFields = headers
    let session = URLSession.shared

    self.delegate?.notGotAnything(nothingReceived: "Checking Condition 
   But not sure what it will receive??")

    let dataTask = session.dataTask(with: request as URLRequest, 
   completionHandler: { (data, response, error) -> Void in
        print("\(response)")
       print("\(data)")
        if (error != nil) {
            print(error)
            self.delegate?.serviceFailed(error: error.debugDescription)
            return

        } else {
            let httpResponse = response as? HTTPURLResponse

            print(httpResponse)

            let parsedData = try? JSONSerialization.jsonObject(with: 
  data!) as! [String:Any]
            let objData = parsedData!["data"] as! [String:Any]

            gotResults = "\(objData)"

            var getActualVal :Bool = false

           self.delegate?.showResults(data: gotResults)

           print (getActualVal)

        }
    })

     dataTask.resume()
}

这是日志:

 Asynchronous wait failed: Exceeded timeout of 20 seconds, with 
unfulfilled expectations: "myExpectation".

/Users/amitsaxena/Desktop/ProjectsiOS/RESTServiceExample/RESTServiceExample/RESTServiceExampleBasicUnitTests.swift:69:错误: - [RESTServiceExampleBasicUnitTests.RESTServiceExampleBasicUnitTests testGetDataFromURL]:XCTAssertNil失败:"错误域= com.apple.XCTestErrorDomain代码= 0 "(空)"" - 测试超时。可选("操作无法完成。(com.apple.XCTestErrorDomain错误0。)") X:异步期望

0 个答案:

没有答案
相关问题