Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 560
Loadex Avatar asked Apr 13 '15 15:04

Loadex


1 Answers

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.

like image 154
bplattenburg Avatar answered Oct 14 '22 15:10

bplattenburg