是否可以模拟应用程序 NSBundle 以在 TDD 期间返回可预测的结果?
例如:
我想测试我的应用程序在文件未保存到 NSBundle 时的处理情况:
//Method to test
func getProfileImage() -> UIImage {
if let profileImagePath = getProfilePhotoPath() {
UIImage(contentsOfFile: profileImagePath)
}
return UIImage(named: "defaultProfileImage")
}
private func getProfilePhotoPath() -> String? {
return NSBundle.mainBundle().pathForResource("profileImage", ofType: "png")
}
是否可以模拟 NSBundle.mainBundle() 以针对 pathForResource 返回 false?
最佳答案
就目前而言,NSBundle.mainBundle()
是一个硬编码的依赖项。我们想要的是指定任何 包的能力,也许默认使用 mainBundle。答案是Dependency Injection .让我们使用构造函数注入(inject)的首选形式,并利用 Swift 的默认参数:
class ProfileImageGetter {
private var bundle: NSBundle
init(bundle: NSBundle = NSBundle.mainBundle()) {
self.bundle = bundle
}
func getProfileImage() -> UIImage {
if let profileImagePath = getProfilePhotoPath() {
return UIImage(contentsOfFile: profileImagePath)!
}
return UIImage(named: "defaultProfileImage")!
}
private func getProfilePhotoPath() -> String? {
return bundle.pathForResource("profileImage", ofType: "png")
}
}
现在测试可以实例化 ProfileImageGetter 并指定它喜欢的任何包。这可能是测试包,也可能是假的。
指定测试包将允许您遇到 profileImage.png 不存在的情况。
指定一个假的会让你 stub 调用 pathForResource()
的结果。
关于ios - 在 Swift TDD 中模拟 NSBundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34898283/