模拟AVCaptureDeviceInput进行测试

时间:2019-05-07 16:48:53

标签: swift testing mocking automated-tests protocols

我正在尝试测试我的应用如何响应不同的AVFoundation配置。我正在使用WWDC视频“ Engineering for Testability”中描述的技术。

我创建了一个协议来表示我的应用使用的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)
    }
}

但是,我永远不会使用符合我的协议的模拟对象成功进行初始化。我该如何解决这个问题才能进行测试?

0 个答案:

没有答案
相关问题