【发布时间】:2016-03-11 18:03:29
【问题描述】:
我是 OCMock 新手,正在尝试对使用 AVCaptureSession 的相机应用程序进行单元测试。
我正在尝试对捕获静止图像并将其传递的方法进行单元测试。
我无法模拟 AVCaptureStillImageOutput 的 captureStillImageAsynchronouslyFromConnection:completionHandler:。 问题在于将CMSampleBufferRef 传递给completionHandler。我并不关心CMSampleBufferRef 的内容是什么,除了它不能为nil/null。对于单元测试用例,它在完成处理程序中被引用的唯一时间是if ( imageDataSampleBuffer ) {...我模拟过的所有其他用途。
这是我尝试过的:
设置:
NSError *error = nil;
CMSampleBufferRef imageDataSampleBuffer;
stillImageOutputMock = OCMStrictClassMock([AVCaptureStillImageOutput class]);
尝试 #1:
[[stillImageOutputMock expect] captureStillImageAsynchronouslyFromConnection:connectionMock
completionHandler:([OCMArg invokeBlockWithArgs:imageDataSampleBuffer, &error, nil])];
给出编译器错误:
/Users/.../UnitTests/Unit/CameraViewController/CameraViewControllerTests.m:195:152: Implicit conversion of C pointer type 'CMSampleBufferRef' (aka 'struct opaqueCMSampleBuffer *') to Objective-C pointer type 'id' requires a bridged cast
Xcode 提议用这个来“修复”它:(我试过了;try #2)
[[stillImageOutputMock expect] captureStillImageAsynchronouslyFromConnection:connectionMock
completionHandler:([OCMArg invokeBlockWithArgs:(__bridge id)(imageDataSampleBuffer), &error, nil])];
但这会生成一个 EXC_BAD_ACCESS,即使我已经模拟了所有在 completionHandler 中实际使用 imageDataSampleBuffer 的方法。异常来自 OCMock,它将 imageDataSampleBuffer 添加到 args 数组中
+ (id)invokeBlockWithArgs:(id)first,... NS_REQUIRES_NIL_TERMINATION
{
NSMutableArray *params = [NSMutableArray array];
va_list args;
if(first)
{
[params addObject:first]; <<<<<< EXCEPTION HERE. first isn't an object
va_start(args, first);
尝试 #3:
OCMock 文档声明 non-object arguments must be wrapped in value objects and the expression must be wrapped in round brackets.,所以我尝试了:
[[stillImageOutputMock expect] captureStillImageAsynchronouslyFromConnection:connectionMock
completionHandler:([OCMArg invokeBlockWithArgs:@(imageDataSampleBuffer), &error, nil])];
但编译器抱怨:
/Users/.../UnitTests/Unit/CameraViewController/CameraViewControllerTests.m:196:119: Illegal type 'CMSampleBufferRef' (aka 'struct opaqueCMSampleBuffer *') used in a boxed expression
建议?
我能够使用[OCMArg invokeBlock] 运行它,例如:
[[stillImageOutputMock expect] captureStillImageAsynchronouslyFromConnection:connectionMock
completionHandler:[OCMArg invokeBlock]];
但随后完成处理程序为 imageDataSampleBuffer 获得 0x0,并且完成处理程序中的所有有趣功能都被跳过。
【问题讨论】:
标签: ios objective-c unit-testing avcapturesession ocmock