我正在尝试针对AVCaptureVideoPreviewLayer
实现的轻按聚焦方法编写测试。我想使用一个模拟AVCaptureDevice
来传递各种条件,然后确保调用了预期的方法来处理实际的聚焦。下面是我正在使用的代码。
NSArray *devices = [AVCaptureDevice devices];
for (AVCaptureDevice *device in devices)
{
if ([device hasMediaType:AVMediaTypeVideo])
{
if ([device position] == AVCaptureDevicePositionBack)
{
CGPoint point = CGPointMake(focus_y, 1-focus_x);
if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error])
{
...
这是我到目前为止的测试代码:
id deviceMock = [OCMockObject niceMockForClass:[AVCaptureDevice class]];
[[[deviceMock stub] andReturnValue:@YES] hasMediaType:AVMediaTypeVideo];
[(AVCaptureDevice*)[[deviceMock stub] andReturnValue:@(AVCaptureDevicePositionBack)] position];
[[[deviceMock stub] andReturnValue:@YES] isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus];
[[[deviceMock stub] andReturnValue:@YES] lockForConfiguration:nil];
[[deviceMock expect] setFocusPointOfInterest:CGPointZero];
[[deviceMock expect] setFocusMode:AVCaptureFocusModeAutoFocus];
[[deviceMock expect] unlockForConfiguration];
[self waitForCompletion:0.55]; //Wait for UI animation
[deviceMock verify]
我想执行三个测试:
所以我想我的问题归结为有一种简单的方法可以存根并返回
[AVCaptureDevice devices]
类方法吗?还是可以安全地假设AVCaptureDevice
上只有一个iPhone/iPad
可以通过上述条件,所以我可以将设备存储为属性并重写我正在测试的方法,以便可以注入模拟AVCaptureDevice
? 最佳答案
您可以对类方法“devices”存根,以返回包含所选内容的数组。
id deviceMock = [OCMockObject niceMockForClass:[AVCaptureDevice class]];
// I think you need to change your return value code to this for the booleans:
[[[deviceMock stub] andReturnValue:OCMOCK_VALUE((BOOL){YES})] hasMediaType:AVMediaTypeVideo];
[[[deviceMock stub] andReturnValue:OCMOCK_VALUE((BOOL){YES})] isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus];
[[[deviceMock stub] andReturnValue:OCMOCK_VALUE((BOOL){YES})] lockForConfiguration:nil];
// No need to wrap the position in an NSNumber
[(AVCaptureDevice*)[[deviceMock stub] andReturnValue:AVCaptureDevicePositionBack] position];
// Stub the class method that returns the devices array
[[[deviceMock stub] andReturn:@[ deviceMock ]] devices];
[[deviceMock expect] setFocusPointOfInterest:CGPointZero];
[[deviceMock expect] setFocusMode:AVCaptureFocusModeAutoFocus];
[[deviceMock expect] unlockForConfiguration];
[self waitForCompletion:0.55]; //Wait for UI animation
[deviceMock verify]
我认为这应该可以帮助您。
关于ios - 用OCMock模拟AVCaptureDevice,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20976970/