gpt4 book ai didi

ios - 使用 OCMock 模拟 AVCaptureDevice

转载 作者:行者123 更新时间:2023-11-28 20:43:57 25 4
gpt4 key购买 nike

我正在尝试为我为 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] 返回 0 个设备并且没有任何反应
  • [AVCaptureDevice 设备] 返回一个不兼容的设备并且没有任何反应
  • [AVCaptureDevice devices] 返回一个兼容的设备,上面的测试代码通过

所以我想我的问题归结为是否有一种简单的方法来 stub 并返回 [AVCaptureDevice devices] 类方法?或者可以安全地假设 iPhone/iPad 上只能有一个 AVCaptureDevice 通过上述条件,所以我可以将设备存储为属性并重写方法我正在测试以便可以注入(inject)模拟 AVCaptureDevice?

最佳答案

您可以 stub 类方法“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com