我在应用程序中有将图片保存在照片库中的功能。我想知道如何测试这段代码:
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
andUIImage
和 sth 出错了,并且有一个错误,completionHandler
所以我最终使用了self.unsucessfullSavingOperation(error)
方法。这当然有单独的测试,但我想介绍的是:
确保每当在相机胶卷中插入图像出现问题时,我最终都会调用此方法
当我尝试调用saveInPhotoGallery()
测试目标时,它会发出警报,表明这需要访问您的照片库(doh!)。但是有一种方法可以在单元测试中跳过此警报或检查它是否弹出并按允许?(就像我说的,对于这个测试,让我们假设我有这个权限)
还是有办法模拟这种行为?