OCMock doesn't stub NSBundle method
Asked Answered
C

1

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 ?

Creepie answered 13/4, 2015 at 15:58 Comment(0)
B
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.

Burmese answered 13/4, 2015 at 19:43 Comment(3)
Thanks for your answer ! I didn't noticed this method before !Creepie
So instead of the code in the question, I use what exactly?Lineage
See the documentation here - 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.Burmese

© 2022 - 2024 — McMap. All rights reserved.