我原来的答案是偏离轨道:OcMOCK不支持这一点!如果你想改变ocmock来支持这个,你需要做一些事情,比如添加一个
BOOL returnValueIsFromInvocation
字段添加到ocmockrecorder,并添加一个设置此项的方法:
- (id)andReturnResultOfInvocation:(NSInvocation *)anInvocation {
returnValueIsFromInvocation = YES;
returnValueIsBoxed = NO;
returnValueShouldBeThrown = NO;
[returnValue autorelease];
returnValue = [anInvocation retain];
return self;
}
然后教
setUpReturnValue
要调用调用(更改以粗体显示):
- (void)setUpReturnValue:(NSInvocation *)anInvocation
{
if (returnValueIsFromInvocation) {
NSInvocation *returnValueInvocation = (NSInvocation *)returnValue;
[returnValueInvocation invoke];
void *buffer = malloc([[anInvocation methodSignature] methodReturnLength]);
[returnValueInvocation getValue:buffer];
[anInvocation setReturnValue:buffer];
free(buffer);
}
else if(returnValueShouldBeThrown)
{
@throw returnValue;
}
else if(returnValueIsBoxed)
{
if(strcmp([[anInvocation methodSignature] methodReturnType],
[(NSValue *)returnValue objCType]) != 0)
[NSException raise:NSInvalidArgumentException
format:@"Return value does not match method signature."];
void *buffer = malloc([[anInvocation methodSignature] methodReturnLength]);
[returnValue getValue:buffer];
[anInvocation setReturnValue:buffer];
free(buffer);
}
else
{
const char *returnType = [[anInvocation methodSignature] methodReturnType];
const char *returnTypeWithoutQualifiers = returnType + (strlen(returnType) - 1);
if(strcmp(returnTypeWithoutQualifiers, @encode(id)) == 0)
[anInvocation setReturnValue:&returnValue];
}
}
引入子类很难实现这种更改,因为必须重写返回ocmockrecorders的方法(例如
stub
,
expect
但ocmockobject的具体子类(occlassmockobject和ocprotocolmockobject)被框架隐藏。