My function under test is very simple:
@implementation MyHandler
...
-(void) processData {
DataService *service = [[DataService alloc] init];
NSDictionary *data = [service getData];
[self handleData:data];
}
@end
I use OCMock 3 to unit test it.
I need to stub the [[DataService alloc] init]
to return a mocked instance, I tried the answer from this question (which is an accepted answer) to stub [[SomeClazz alloc] init]
:
// Stub 'alloc init' to return mocked DataService instance,
// exactly the same way as the accepted answer told
id DataServiceMock = OCMClassMock([DataService class]);
OCMStub([DataServiceMock alloc]).andReturn(DataServiceMock);
OCMStub([DataServiceMock init]).andReturn(DataServiceMock);
// run function under test
[MyHandlerPartialMock processData];
// verify [service getData] is invoked
OCMVerify([dataServiceMock getData]);
I have set break point in function under test, I am sure [service getData]
is called when run unit test, but my above test code (OCMVerify) fails. Why?
Is it because the function under test is not using my mocked DataService
? But the answer accepted in that question tells it should work. I get confused now...
I want to know how to stub [[SomeClazz alloc] init]
to return mocked instance with OCMock?