How to lock a class method using OCMockito?

The OCMockito documentation claims that it's possible to make fun of class objects , but I'm damned if I can how to do this. The following test failed Expected "Purple" but was "":

- (void)testClassMethodMocking
{
    Class stringClassMock = mockClass([NSString class]);
    [given([stringClassMock string]) willReturn:@"Purple"];

    assertThat([NSString string], equalTo(@"Purple"));
}

If the answer to the section with frequently asked questions is "How do you mock a class object?" doesn't mean it's possible to stub the return value of a class method, what is it used for?

EDIT:

Of course, the above is a degenerate example, the real call [NSString string] is inside the test method:

- (NSString *)methodThatCallsAClassMethod
{
    return [NSString string];
}

... to make the statement above:

assertThat([testedObject methodThatCallsAClassMethod], equalTo(@"Purple"));

So the above just doesn't work? How then can I achieve what I want to do?

+2
1

NSString. mock:

assertThat([stringClassMock string], equalTo(@"Purple"));

, NSString. , .

EDIT:

methodThatCallsAClassMethod NSString. , , .

:

return [self.stringClass string];

stringClass .

:

@interface TestingFoo : Foo
@end

@implementation @TestingFoo

- (NSString *)methodThatCallsAClassMethod
{
    // return whatever you want
}

@end
+4

All Articles