如何在Swift 4中测试API?

时间:2018-11-02 10:34:33

标签: ios swift xcode unit-testing

我是单元测试的新手。我已经测试了很多功能并且理解了这个概念,现在我想检查一下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()停下来,因此,我无法测试errors或预期的results。有帮助吗?

1 个答案:

答案 0 :(得分:0)

这里有一个完成处理程序,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)
        }