9

OCMock 提供了一个很好的语法来模拟类方法,但是类方法调用可以转发到真正的类对象吗?

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

4

1 回答 1

0

子类 OCMock 并覆盖 forwardingTargetForSelector:(SEL)selector:

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

(来源:“如何在 Objective-C 中转发类方法?”

于 2016-06-17T16:36:33.337 回答