Search code examples
objective-cunit-testingnsbundlexctestocmock

OCMock doesn't stub NSBundle method


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 ?


Solution

  • 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.