所以我在我的应用程序的单元测试中运行异步代码时遇到问题。我正在使用期望以等待代码在测试完成之前执行。异步代码通过 heroku 运行以获取值,然后应在应用程序中返回它们。通过这个单元测试,我试图确保通过 heroku 的连接正常工作。下面是我的代码:
func test() {
let url = "https://s.herokuapp.com/test"
let params: [String: Any] = ["account_id": AppState.sharedInstance.user.accounttoken]
let expectation = self.expectation(description: "Testing returning value")
let totalBalance = ""
Alamofire.request(url, method: .post, parameters: params)
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result {
case .success:
print("Returned with success")
case .failure(let error):
let status = response.response?.statusCode
print("Failed, status: \(status)")
print("Here is the error: \(error)")
}
if let result = response.result.value {
let balance = result as! NSDictionary
let totalBalance = String(describing: "\(balance["Balance"]!)")
}
XCTAssert(totalBalance != "")
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
XCTAssert(totalBalance != "")
}
我感到困惑的原因是因为我在实际应用程序中让异步代码返回值没有错误。我在单元测试中只有等待时间问题。我收到两个失败错误,一个是 XCTAssert 不正确,另一个是 waitForExpectations 超过 10 秒。如果这有助于找到解决方案,也会出现一些错误:
以下是文本形式的错误信息:
2019-07-01 09:44:38.181971-0400 Spotbirdparking[49677:4306598] TIC TCP Conn Failed [6:0x6000030b7cc0]: 3:-9816 Err(-9816) 2019-07-01 09:44:38.188607-0400 Spotbirdparking[49677:4306598] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9816) 2019-07-01 09:44:38.188819-0400 Spotbirdparking[49677:4306598] Task .<1> HTTP load failed (error code: -1200 [3:-9816]) 2019-07-01 09:44:38.189215-0400 Spotbirdparking[49677:4306623] Task .<1> finished with error - code: -1200 /Users/drewloughran/Desktop/SpotBird/SpotbirdparkingTests/SpotbirdparkingTests.swift:117: error: -[SpotbirdparkingTests.SpotbirdparkingTests test_stripe] : Asynchronous wait failed: Exceeded timeout of 10 seconds, with unfulfilled expectations: "Testing stripe returning value".
我也是 swift 的新手,所以对于这个问题,我们将不胜感激。
最佳答案
德鲁,请先检查这个错误。您的请求无法加载。
HTTP load failed (error code: -1200 [3:-9816]) 2019-07-01 09:44:38.189215-0400 Spotbirdparking[49677:4306623] Task .<1> finished with error - code: -1200
还要确保您用来满足期望的 block 正在执行。可能不是,这就是你的期望没有实现并且你的测试失败的原因。
此外,from this documentation我可以看到您的验证与自动验证案例相交。这是多余的。
Response Validation
By default, Alamofire treats any completed request to be successful, regardless of the content of the response. Calling validate before a response handler causes an error to be generated if the response had an unacceptable status code or MIME type.
Manual Validation
Alamofire.request("https://httpbin.org/get")
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.response { response in
switch response.result {
case .success:
print("Validation Successful")
case .failure(let error):
print(error)
}
}
Automatic Validation
Automatically validates status code within 200...299 range, and that the Content-Type header of the response matches the Accept header of the request, if one is provided.
Alamofire.request("https://httpbin.org/get").validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful")
case .failure(let error):
print(error)
}
}
祝你好运!
关于swift - 在单元测试中运行异步代码的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56837035/