swift - 模拟 AVCaptureDeviceInput 进行测试
问题描述
我正在尝试测试我的应用程序如何响应不同的 AVFoundation 配置。我正在使用 WWDC 视频“可测试性工程”中描述的技术。
我创建了一个协议来表示AVCaptureDevice
我的应用程序使用的各个部分。
public protocol AVCaptureDeviceProperties: class {
//MARK: Properties I use and need to test
var position: AVCaptureDevice.Position { get }
var focusMode: AVCaptureDevice.FocusMode { get set }
var exposureMode: AVCaptureDevice.ExposureMode { get set }
var whiteBalanceMode: AVCaptureDevice.WhiteBalanceMode { get set }
//MARK: Functions I use use and need to test
func lockForConfiguration() throws
func unlockForConfiguration()
func isFocusModeSupported(_ focusMode: AVCaptureDevice.FocusMode) -> Bool
func isExposureModeSupported(_ exposureMode: AVCaptureDevice.ExposureMode) -> Bool
func isWhiteBalanceModeSupported(_ whiteBalanceMode: AVCaptureDevice.WhiteBalanceMode) -> Bool
}
我有一个AVCaptureDevice
符合我的协议的扩展。
extension AVCaptureDevice: AVCaptureDeviceProperties {
//Don't need anything because AVCaptureDevice already has implementations of all the properties and functions I use.
}
我现在可以为自己创建一个对象,我可以在其中为不同的测试用例配置所有属性。效果很好!
但是,我需要更进一步并获得一个模拟AVCaptureDeviceInput
对象。这个对象只有一个初始化器,AVCaptureDevice
但我希望能够使用我的协议类型模拟初始化。到目前为止,我有这个:
extension AVCaptureDeviceInput {
convenience init?(device: AVCaptureDeviceProperties) throws {
guard let downcast = device as? AVCaptureDevice else {
return nil
}
try self.init(device: downcast)
}
}
但是,我永远不会使用符合我的协议的模拟对象成功初始化。如何解决这个问题以便我可以测试?
解决方案
推荐阅读
- python - 替换数据框中的单个字符?
- javascript - 为什么我从 post request express 收到 undefined
- python - 我怎样才能阻止错误的行进入霍夫空间
- reactjs - 将承诺的返回值分配给反应中的外部变量
- powerbi - DAX 表达式的性能问题
- xml - ImportXML Google Sheets 生成导入的内容为空
- javascript - 我可以得到没有点的 document.URL 吗?
- javascript - 观察 ember 数据记录时点击后集成测试不渲染
- html - CSS div 未放置在父级的右侧
- node.js - 使用 Jest Supertest Formidable 测试端点时文件未以多部分形式发送