How to stub class methods?

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

How to stub class methods?

Postby deforge » 06 May 2013, 18:26

I was excited when I saw the feature of stubbing class methods, but I can't get it working. I've tried a very simple thing. I created a TestClass with only one class method:
Code: Select all
@interface TestClass : NSObject
+ (NSString *)returnSomeString;

@implementation TestClass
+ (NSString *)returnSomeString
    return @"AAA";

Then I wrote the following test
Code: Select all
@interface TestClassTest : SenTestCase

@implementation TestClassTest
- (void)testClassMethod
    id mock = [OCMockObject mockForClass:[TestClass class]];

    [[[mock stub] andReturn:@"123"] returnSomeString];

    NSString *str = [TestClass returnSomeString];

    NSLog(@"%@", str);

The test method prints "AAA" but not "123".
I guess I'm missing something. What am I doing wrong?
Posts: 1
Joined: 06 May 2013, 18:15

Re: How to stub class methods?

Postby erik » 08 May 2013, 14:46

What you're doing should work. To confirm, I've just taken your TestClass and the test, and for me it does print "123". Are you sure you're linking against the right version of OCMock? Maybe you have an old version somewhere?
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany

Return to OCMock