iphone – 在访问OCMock中的参数时,EXC_BAD_ACCESS
我试图用OCMock的存根和方法写一个代码块.
在这种情况下,UIImageView扩展类正在测试.我想检查扩展名是否调用[self setImage:],其参数为非零(稍后将使用其他图像比较). 当使用OCMock和方法时,块完成后,测试将以EXC_BAD_ACCESS崩溃. id mockView = [OCMockObject mockForClass:[UIImageView class]]; [[[mockView stub] andDo:^(NSInvocation *invocation) { UIImage *img; [invocation getArgument:&img atIndex:2]; <---- line causing the exception somebodySetImage |= (img != nil); }] setImage:OCMOCK_ANY]; [mockView do_something_that_calls_setImage]; 我现在找到的唯一解决方案是使用andCall而不是andDo,但这使测试复杂化. 我可以避免碰撞吗? UPDATE - (void)testDownloadingThumbnail { PInfo *_sut = [[PInfo alloc] init]; __block id target = nil; id mock = [OCMockObject mockForClass:[NSOperationQueue class]]; [[[mock expect] andDo:^(NSInvocation *inv) { NSInvocationOperation *op; [inv getArgument:&op atIndex:2]; target = [[op invocation] target]; /* replacing this line with STAssert does not help either */ }] addOperation:OCMOCK_ANY]; [_sut setDownloadQueue:mock]; [_sut startDownloadingImagesAsync:YES]; [mock verify]; STAssertEqualObjects(target,_sut,@"invalid op target"); } 这是测试代码(PInfo的单一方法): - (void)startDownloadingImagesAsync:(bool)isThumbnailImg { NSInvocationOperation *inv; inv = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadThumbnailWorker:) object:nil]; [[self downloadQueue] addOperation:inv]; } 从EXC_BAD_ACCESS的startDownloadingImagesAsync退出时代码仍然崩溃. 然而,如果我在块中使用getArgument,那么我会尝试做任何事情. 附:感谢帮助. 解决方法
使用NSProxy的forwardInvocation:方法时遇到类似的问题.
你能试试下面吗? NSInvocationOperation *op; // Change this line __unsafe_unretained NSInvocationOperation *op; // to this line 或者另一种方法可能是保留NSInvocation的参数: [invocation retainArguments]; http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html#//apple_ref/occ/instm/NSInvocation/retainArguments 我稍后再尝试添加一个更详细的解释. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |