9

OCMock offers a nice syntax to mock class methods, but can class method calls be forwarded to the real class object?

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];

The problem is andForwardToRealObject only works for partials. I could do the method swizzling myself, but is there an easy way for me to call the original class method using 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 に答える