gpt4 book ai didi

ios - 如何在 Swift 4 中测试 API?

转载 作者:行者123 更新时间:2023-11-28 05:50:37 25 4
gpt4 key购买 nike

我是单元测试的新手。我已经测试了很多功能并且我理解了这个概念,现在我想检查 API。是否可以?大概吧。这是 API:

func sendRequest(path: String, params: Dictionary<String, Any>, showSpinner: Bool, completionHandler: @escaping (JSON, Error?) -> Void) {
if Constants.IS_SIMULATOR {
print("Path: \(path)")
print("Params: \(params)")
}

if Constants.APP_DEL.reachability?.connection == .none {
completionHandler(JSON.null, NSError(domain: "No internet", code: 4, userInfo: nil))
return
}

UIApplication.shared.isNetworkActivityIndicatorVisible = true

if showSpinner {
HUD.show(.labeledProgress(title: "Loading...", subtitle: "Please wait"))
}

if let jsonData = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) {

let url = NSURL(string: String(format: "%@%@", Constants.TEST_URL, path))!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = jsonData
request.timeoutInterval = 120

let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
DispatchQueue.main.async {
if error != nil {
print(" ........ \(String(describing: error?.localizedDescription))")
UIApplication.shared.isNetworkActivityIndicatorVisible = false

if showSpinner {
HUD.flash(.labeledError(title: "Server issue", subtitle: "Invalid response"), delay: 2.0)
}

completionHandler(JSON.null, NSError(domain: "Invalid response", code: 420, userInfo: nil))
return
}


if (data?.isGzipped)! {
let decompressedData: Data = try! data!.gunzipped()
var json: JSON = JSON.null
do {
json = try JSON(data: decompressedData)
}
catch {
print(error)
}

if Constants.IS_SIMULATOR {
print("Response: \(json)")
}

UIApplication.shared.isNetworkActivityIndicatorVisible = false


if json["status"].int == 200 {
if showSpinner {
HUD.flash(.success, delay: 0.5)
}
completionHandler(json["data"], nil)
}
else if json["status"].int == 202 {
if showSpinner {
HUD.hide()
}
completionHandler(JSON.null, NSError(domain: json["message"].string!, code: json["status"].int!, userInfo: nil))
}
else if json["status"].int == 310 {
if showSpinner {
HUD.hide()
}
completionHandler(json["data"], nil)
}
else if json["status"].int == 403 {
if showSpinner {
HUD.hide()
}

GeneralHelper.sharedInstance.displayAlertMessage(titleStr: "Session expired", messageStr: "Kindly login again.")

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {

let domain = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: domain)
UserDefaults.standard.synchronize()
Constants.APP_DEL.navC?.popToRootViewController(animated: false)
})
completionHandler(JSON.null, NSError(domain: json["message"].string!, code: json["status"].int!, userInfo: nil))
}
else {
if showSpinner {
HUD.flash(.labeledError(title: "", subtitle: json["message"].string!), delay: 2.0)
}
completionHandler(JSON.null, NSError(domain: json["message"].string!, code: json["status"].int!, userInfo: nil))
}
}
else {
let backToString = String(data: data!, encoding: String.Encoding.utf8) as String?
if Constants.IS_SIMULATOR {
print("Invalid response: \(String(describing: backToString))")
}
UIApplication.shared.isNetworkActivityIndicatorVisible = false


if showSpinner {
HUD.flash(.labeledError(title: "Server issue", subtitle: "Invalid response"), delay: 2.0)
}
completionHandler(JSON.null, NSError(domain: "Invalid response", code: 420, userInfo: nil))
}
}
}

task.resume()
}
}

所以,为了测试这个,我做了这个:

func testAPIWorking() {
params = ["ios_token": "dq6YJkKwEx0:APA91bFeOTfJRFd5G78xMkv3AvjSLA7ey2dJxTZZAtMuuC50CqWILNzNjdgqVpNpDn7R4I0DLoydIVDYKubpGfgfu1bwz1H3VNU4D88ek8PJTAjxrd3CWkW78g0sNv6EZDLlTqUFeNxh", "api_token": "kfRSHL0bVP1fSmxNY3NfEGs8g0ktKCbTsPRRbfarh3a5ISIcZLu3qdK07MJ9H4rJ", "player_id": 8083]

ServiceHelper.sharedInstance.sendRequest(path: "home", params: self.params, showSpinner: false) { (result, error) in
if error != nil {
XCTFail("Fail")
}
else {

}
}
}

我在任务处添加了一个断点,它打印了任务,但是当我尝试移动到下一行时,它没有进入 Dispatch,而是让我退出并在 处停止task.resume(),因此我无法测试错误或预期的结果。有帮助吗?

最佳答案

这里有一个完成处理程序,api 调用不同步。所以你应该在你的测试中等待结果。在 Xcode 中,您可以使用 XCTestExpectation

例如:

    func testAPIWorking()
{

let expectation = XCTestExpectation.init(description: "Your expectation")
params = ["ios_token": "dq6YJkKwEx0:APA91bFeOTfJRFd5G78xMkv3AvjSLA7ey2dJxTZZAtMuuC50CqWILNzNjdgqVpNpDn7R4I0DLoydIVDYKubpGfgfu1bwz1H3VNU4D88ek8PJTAjxrd3CWkW78g0sNv6EZDLlTqUFeNxh", "api_token": "kfRSHL0bVP1fSmxNY3NfEGs8g0ktKCbTsPRRbfarh3a5ISIcZLu3qdK07MJ9H4rJ", "player_id": 8083]

ServiceHelper.sharedInstance.sendRequest(path: "home", params: self.params, showSpinner: false) { (result, error) in

if error != nil
{
XCTFail("Fail")
}
// The request is finished, so our expectation
expectation.fulfill()
}
// We ask the unit test to wait our expectation to finish.
self.waitForExpectations(timeout: 20)
}

关于ios - 如何在 Swift 4 中测试 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53116903/

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