OCMock为mock类方法提供了一个很好的语法,但类方法调用可以转发到真正的类对象吗?
self.connectionMock = [OCMockObject mockForClass:NSURLConnection.class];
[[[[self.connectionMock stub] andDo:^(NSInvocation *invocation) {
NSURL *url = [invocation getArgumentAtIndexAsObject:2];
NSLog(@"Passing through mock: %@", url);
} ] andForwardToRealObject] connectionWithRequest:OCMOCK_ANY delegate:OCMOCK_ANY];
问题是andForwardToRealObject
仅适用于部分。我可以自己调整方法,但是有一种简单的方法可以让我使用OCMock调用原始的类方法吗?
答案 0 :(得分:0)
子类OCMock并覆盖forwardingTargetForSelector:(SEL)选择器:
+(id)forwardingTargetForSelector:(SEL)selector
{
Class yourClass = [classObject class];
return yourClass;
}