Luke
Luke

Reputation: 9700

Stub a class method on an OCMPartialMock?

I’m using OCMock, and attempting to test a class with two class methods. I’d like to stub one in order to write a test for the other, so I’d normally use an OCMPartialMock. However, if I do this, I can’t call the class method since OCMPartialMock uses an instance of the class, not the class itself. If I use OCMClassMock, it’ll lose the implementation of the method I want to test.

In summary: I have two class methods, and I’d like to stub one but retain the ability to call the other, using OCMock. How can I achieve this?

Upvotes: 1

Views: 734

Answers (2)

aklee
aklee

Reputation: 1

sample code:

@interface MyClass: NSObject

+ (void)hello;//this is a class method

@end



id mock = OCMClassMock([MyModel class]);

[OCMStub([mock hello]) andDo:^(NSInvocation *invocation) {
      NSLog(@"hello everyone"); 
 }];

[MyModel hello];    //it will print  'hello everyone'
    

Upvotes: 0

Luke
Luke

Reputation: 9700

Found the answer: need to use an OCMClassMock, and since it’s swizzled the class, call the other class method on the class itself, not on my mocked id version.

Upvotes: 2

Related Questions