OCMock转发到原始类方法

时间:2013-10-25 00:10:03

标签: objective-c ocmock

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调用原始的类方法吗?

1 个答案:

答案 0 :(得分:0)

子类OCMock并覆盖forwardingTargetForSelector:(SEL)选择器:

+(id)forwardingTargetForSelector:(SEL)selector
{
    Class yourClass = [classObject class];
    return yourClass;
}

(信用:"How can I forward class methods in Objective-C?"