gpt4 book ai didi

ios - 快速对需要钥匙串(keychain)身份验证的私有(private)函数进行单元测试

转载 作者:行者123 更新时间:2023-11-28 08:26:49 24 4
gpt4 key购买 nike

提前感谢您的任何建议!

我正在为 iOS 开发快速设置一些单元测试。该方法需要调用钥匙串(keychain)来创建 authToken 才能成功运行该函数。我不确定如何为这种环境创建单元测试。我是否模拟了可用于绕过身份验证的本地文件?我是否尝试完全跳过身份验证步骤?

我正在测试的函数也是一个私有(private)函数,我很难概念化如何通过公共(public)方法测试它。这是我正在使用的代码:

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() 间接执行此操作,因为它们是私有(private)的。

我希望能够测试 statusCode 是否正在从服务器正确关闭,如果它返回为 200 或 401,则执行适当的步骤。

我不一定要寻找完整的代码,而只是寻找解决问题的方法。如果您知道可能有用的资源,我将不胜感激。我对此很陌生,我尝试在线查看资源但找不到任何东西。我发现很多情况是您不想直接测试私有(private)函数,也不建议为了测试而将函数更改为公共(public)函数。

再次感谢您的关注!

肖恩·W.

最佳答案

在测试类中定义该私有(private)方法,具有相同的签名。只需尝试调用该方法,它就会调用您的实际类方法。

关于ios - 快速对需要钥匙串(keychain)身份验证的私有(private)函数进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773648/

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