NSRover
NSRover

Reputation: 978

OCMock stub class method for all invocations

I have a Utility class with some class methods.

@interface DataValidator : NSObject

+ (BOOL)foo;

@end

The usage of the class is inside other classes, say NetworkManager, DBHandler etc. And since there are no instance methods in Utility class, there is no need for any class to create an instance of Utility class. Rather they directly make the calls like this

[Utility foo];

When writing tests for NetworkManager/DBHandler, is it possible to mock Utility and stub foo so all calls made to it return the mocked response.

If I mock the Utility class in NetworkManager class tests, the NetworkManager code still calls original method instead of the stubbed one.

This only works if I directly call [Utility foo] from inside the tests, but thats not useful in my case.

Upvotes: 1

Views: 1256

Answers (2)

Maksym Bondarenko
Maksym Bondarenko

Reputation: 404

Looks like OCMock 3 has a way to mock Class methods: http://ocmock.org/reference/#mocking-class-methods An example from that page:

id classMock = OCMClassMock([SomeClass class]);
OCMStub([classMock aClassMethod]).andReturn(@"Test string");
// result is @"Test string"
NSString *result = [SomeClass aClassMethod];

Upvotes: 1

NSRover
NSRover

Reputation: 978

I wasn't able to achieve this. What I ended up doing is converting the class methods to instance methods and then mocking those.

Upvotes: 0

Related Questions