6

I'm using since recently OCMock for my unit testing. I need to stub the objectForInfoDictionaryKey: method from NSBundle. I've done the following :

self.bundleMock = OCMClassMock([NSBundle class]);
OCMStub([self.bundleMock objectForInfoDictionaryKey:@"GITHash"]).andReturn(@"c424242");
;

Here is the call I wanna stub :

NSString * appHashString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"GITHash"];

But nothing seem to be stubbed, at runtime I still have the "correct" value.

What did I do wrong ?

Loadex
  • 1,502
  • 1
  • 12
  • 25

1 Answers1

7

I could be misremembering, but I think you need to use partialMockForObject to mock the instance returned by [NSBundle mainBundle], instead of mocking the class NSBundle because objectForInfoDictionaryKey is an instance method, not a class method.

bplattenburg
  • 623
  • 1
  • 8
  • 33
  • Thanks for your answer ! I didn't noticed this method before ! – Loadex Apr 14 '15 at 07:59
  • So instead of the code in the question, I use what exactly? – Jeff Feb 23 '16 at 22:38
  • 1
    See the documentation here - http://ocmock.org/reference/#partial-mocks In keeping with the original question's code: `self.bundleMock = OCMPartialMock([NSBundle mainBundle]); OCMStub([self.bundleMock objectForInfoDictionaryKey:@"GITHash"]).andReturn(@"c424242");` This mocks the instance so that calling an instance method returns the stubbed return value. – bplattenburg Feb 25 '16 at 19:22