2016-09-29 74 views
1

在此先感謝您的任何建議!單元測試在swift中需要鑰匙串驗證的私有函數

我正在爲iOS開發設置一些swift的單元測試。該方法需要調用鑰匙串來創建authToken以成功運行該函數。我不確定如何爲這種環境創建單元測試。我是否模擬了可用於繞過身份驗證的本地文件?我是否嘗試完全跳過驗證步驟?

我測試的函數也是一個私有函數,我很難概念化如何通過公共方法來測試它。這裏是我正在使用的代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.setMyDoctorsNavBarTitle() 
    self.setBackgroundWaterMark() 
    self.getDoctors() 
    //self.refreshControl?.addTarget(self, action: #selector(MyDoctorsViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged) 

} 

private func getDoctors() { 
    let authToken: [String: AnyObject] = [ "Authorization": keychain["Authorization"]!, // creates an authToken with the current values stored in 
              "UUID": keychain["UUID"]!, "LifeTime": keychain["LifeTime"]! ] // the keychain 

    RestApiManager.sharedInstance.postMyDocs(authToken) { (json, statusCode) in // passes the created authToken to postMyDocs in RestAPI to see if 
     if statusCode == 200 {                    // the token matches what's on the server 
      if let results = json["Doctors"].array { // If the credentials pass, we grab the json file and create an array of Doctors 
       for entry in results { 
        self.buildDoctorObject(entry) // Doctors information is parsed into individual objects 
       } 
      } 
     } else if statusCode == 401 { 
      /* If statucCode is 401, the user's AuthToken has expired. The historical AuthToken data will be removed from the iOS KeyChain and the user will be redirected to the login screen to reauthorize with the API 
      */ 
      self.keychain["Authorization"] = nil 
      self.keychain["UUID"] = nil 
      self.keychain["LifeTime"] = nil 

      let loginController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController 

      NSOperationQueue.mainQueue().addOperationWithBlock { 
       self.presentViewController(loginController, animated: true, completion: nil) 
      } 

     } else if statusCode == 503 { 
      print("Service Unavailable Please Try Again Later") 
     } 

    } 

} 

private func buildDoctorObject(json: JSON){ 
    let fName = json["FirstName"].stringValue 
    let lName = json["LastName"].stringValue 
    let city = json["City"].stringValue 
    let phNum = json["PhoneNumber"].stringValue 
    let faxNum = json["FaxNumber"].stringValue 
    let state = json["State"].stringValue 
    let addr = json["Address"].stringValue 
    let img = json["Image"].stringValue 
    let zip = json["Zip"].stringValue 
    let tax = json["Taxonomy"].stringValue 

    let docObj = DoctorObject(fName: fName, lName: lName, addr: addr, city: city, state: state, zip: zip, phNum: phNum, faxNum: faxNum, img: img, tax: tax) 

    self.docs.append(docObj) 
    self.tableView.reloadData() 
} 

我希望能夠單元測試getDoctors()和buildDoctorObject()函數,但我只能做到這一點間接通過viewDidLoad中(),因爲它們是私人的。

我希望能夠以測試該的StatusCode正在從服務器放倒正確和適當的步驟發生,如果它回來爲200或401

我不一定要找完整的代碼,而只是一種解決問題的方法。如果您知道可能有用的資源,我將不勝感激。我對此很陌生,我嘗試在網上查找資源,但找不到任何東西。我發現的很多是你不想直接測試私有函數,也不建議爲了測試而將函數改爲public。

再次感謝您的關注!

Sean W.

回答

0

定義測試類中的私有方法,具有相同的簽名。只要嘗試調用該方法就會調用實際的類方法。

+0

所以基本上覆制並粘貼測試類中的私有方法?還有一些其他方法需要我正在工作,我基本上是將整個viewcontroller複製到測試類中嗎? 感謝您的建議! –