Swift,回调功能似乎不适用于单元测试

时间:2016-05-10 10:29:14

标签: unit-testing swift2 xctest

我是Unit测试的新手,并尝试调用Callback函数。 但它绕过了这个功能并且即将结束。

以下代码

 func login(txtPassword: String,txtUsername : String) -> Bool
{

      var testReturn = true

 //               dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){
//                
//                dispatch_async(dispatch_get_main_queue()) {

            self.netUtil.checkNetworkConnection() {(isConnected) -> Void in
                print("Service Connected : \(isConnected)")
                if(isConnected){

                    var dictionary:Dictionary<String,String> = Dictionary<String,String>()
                    dictionary[self.constants.defaultsKeys.EMAIL] = txtUsername
                    dictionary[self.constants.defaultsKeys.PASSWORD] = txtPassword
                    let jsonStr = self.fileMgr.getJsonBase64String(dictionary)

                    print("USER JSON STRING:\(jsonStr)")

                    let urlStr = "\(self.constants.SERVER_URL)\(self.constants.AUTHENTICATE_URL)"

                    self.netUtil.callLoginPostParamWebservice(urlStr,urlParam: jsonStr) {(dataDictionary) -> Void in
                        if let errorStr = dataDictionary[self.constants.defaultsKeys.RESPONSE_ERROR] {
                            if(errorStr.isEmpty || errorStr==""){

                                if dataDictionary[self.constants.defaultsKeys.RESPONSE_STATUS] == self.constants.defaultsKeys.RESPONSE_SUCCESS {

                                    self.fileMgr.setCacheData(txtUsername, key: self.constants.defaultsKeys.EMAIL)
                                    self.fileMgr.setCacheData(txtPassword, key: self.constants.defaultsKeys.PASSWORD)

                                    if let tokenStr = dataDictionary[self.constants.defaultsKeys.RESPONSE_MESSAGE] {
                                        self.fileMgr.setCacheData(tokenStr, key: self.constants.defaultsKeys.TOKEN_KEY)
                                    }

                                    if let userId = dataDictionary[self.constants.defaultsKeys.SURVEYOR_ID_KEY]{
                                        self.fileMgr.setCacheData(userId, key: self.constants.defaultsKeys.SURVEYOR_ID_KEY)
                                    }

                                    let userName = dataDictionary[self.constants.defaultsKeys.SURVEYOR_NAME_KEY]
                                    self.fileMgr.setCacheData(userName!, key: self.constants.defaultsKeys.SURVEYOR_NAME_KEY)

                                    self.fileMgr.setCacheData(self.constants.defaultsKeys.LANGUAGE_ENGLISH, key: self.constants.defaultsKeys.DEFAULT_LANGUAGE)



                                    let urlStr: String = self.constants.SERVER_URL + "" + self.constants.LABELS_URL

                                    self.netUtil.callLoginPostParamWebservice(urlStr,urlParam:jsonStr) {(dataDictionary) -> Void in

                                        let errorStr = dataDictionary[self.constants.defaultsKeys.RESPONSE_ERROR]!
                                        let data = dataDictionary[self.constants.defaultsKeys.RESPONSE_RESULT]
                                        if(errorStr.isEmpty || errorStr==""){
                                            let labelFileName = self.fileMgr.getLabelsFileName()
                                            self.fileMgr.writeFile(data!, fileName: labelFileName,documentDir: self.fileMgr.getDocumentPath())

                                            self.fileMgr.setCacheData(self.fileMgr.getLabelsDictionary(self.constants.defaultsKeys.LANGUAGE_ENGLISH), key: self.constants.defaultsKeys.LABELS_ENGLISH_DICTIONARY)

                                            self.fileMgr.setCacheData(self.fileMgr.getLabelsDictionary(self.constants.defaultsKeys.LANGUAGE_ENGLISH), key: self.constants.defaultsKeys.LABELS_ENGLISH_DICTIONARY)


                                        }
                                        else{

                                                testReturn = false
                                        }
                                    }
                                }
                                else{
                                    testReturn = false

                                }
                            }
                            else{
                                testReturn = false


                            }
                        }
                    }
                }
                else{
                    testReturn = false
                }
              }

           return testReturn
}

  func testExample() {
      // This is an example of a functional test case.

       XCTAssertTrue(login("122!2016",txtUsername: "deval@im.com"))
  }

0 个答案:

没有答案