【发布时间】:2017-12-07 17:52:22
【问题描述】:
我在应用程序中有将图片保存在照片库中的功能。我想知道如何测试这段代码:
func saveInPhotoGallery() {
guard self.cameraOutput != nil else { return }
if self.cameraOutput is UIImage {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: (self.cameraOutput as? UIImage)!)
}, completionHandler: { (saved, error) in
guard error == nil else {
self.unsucessfullSavingOperation(error)
return
}
})
}
}
现在假设我想在我的案例场景中测试self.cameraOutput 是和UIImage 和某事出错并且completionHandler 中存在错误所以我最终使用了self.unsucessfullSavingOperation(error) 方法。这当然有单独的测试,但我想介绍的是:
确保每当在相机胶卷中插入图像出现问题时,我都会调用此方法
当我尝试在测试目标中调用 saveInPhotoGallery() 时,它会发出警报,表明这需要访问您的照片库(哦!)。但是有一种方法可以在 Unit Tests 中跳过此警报,或者检查它是否弹出并按 allow? (就像我说的,对于这个测试,假设我有这个权限)
或者有办法模拟这种行为?
【问题讨论】:
标签: ios swift xctest xctestcase