This Is My Blog

There are many like it, but this one is mine.

Class Methods in OCMock

I’m opening my blog with a post about patches I’ve made to OCMock to handle mocking and stubbing class methods. You can find my fork HERE.

I built on top of the work in progress in the OCMock master. It works, but will probably need some refinement.

Let’s the example of a static constructor in NSString:

NSString *s = [NSString string];    // returns empty string

In order to mock this, we do this:

id mockStringClass = [OCMockObject mockForClassObject:[NSString class]];
NSString *mockString = @"Mocked String Result";
[[[mockStringClass stub] andReturn:mockString] string];
NSString *actualString = [NSString string];
STAssertTrue(0 != [actualString length], @"Expected String Not Be Empty");

I sent the pull request about two weeks ago, still waiting to hear back. Read on if you’re interested in the details…