How do you mock out private properties with OCMock for iOS?

Shiun picture Shiun · Aug 10, 2012 · Viewed 7.1k times · Source

I have a private property that is declared in the .m file of my class to be tested, let's call it ClassUnderTest. ClassUnderTest instantiates an instance of ClassToBeMocked. How do I use OCMock to mock out an instance of the ClassToBeMocked and assign it to the ClassUnderTest?

Answer

Christopher Pickslay picture Christopher Pickslay · Aug 10, 2012

Re-declare the property in your test class. You can do the same for private methods. In ClassUnderTestTest.m:

@interface ClassUnderTest ()

@property(retain)ClassToBeMocked *instanceToBeMocked;

-(void)somePrivateMethod;

@end