HTTPResult *successResult = [[HTTPResult alloc] init];
successResult.success = YES;
successResult.content = @{@"key":@"value"};
id httpMock = OCMClassMock([HTTPUtility class]);
OCMStub(ClassMethod([httpMock requestWithHTTPMethod:HTTPRequestMethodGet
URLString:@"testURL"
parameters:[OCMArg any]
response:[OCMArg any]])).andDo(^(NSInvocation *invocation) {
void(^response)(HTTPResult *) = nil;
[invocation getArgument:&response atIndex:5];
response(successResult);
});
这会在[OCMockObject dealloc]方法中抛出EXC_BAD_ACCESS,并在调用类方法时崩溃
使用特定块测试类方法的正确方法
答案 0 :(得分:0)
看起来你想要捕获传递给类方法的响应参数块(不知道我不能确定的方法的签名),所以你可以检查参数,而不是使用[OCMArg any]
。一块。请参见第4.3节here。
[OCMArg checkWithBlock:^BOOL(id value) { /* return YES if value is ok */ }]
所以在你的例子中:
OCMStub(ClassMethod([httpMock requestWithHTTPMethod:HTTPRequestMethodGet
URLString:@"testURL"
parameters:[OCMArg any]
response:[OCMArg checkWithBlock:^BOOL(HTTPResult *response) {
response(successResult);
return YES; // Replace this with a check for whether response is valid.
}]);