My tested function is very simple:
@implementation MyHandler ... -(void) processData { DataService *service = [[DataService alloc] init]; NSDictionary *data = [service getData]; [self handleData:data]; } @end
I use OCMock 3 before unit test.
I need to [[DataService alloc] init] in order to return the scoffed instance , I tried the answer from this question (which is the 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 set a breakpoint in the function under test, I am sure that [service getData] is called when unit test starts, but my test code (OCMVerify) described above fails. What for?
Is it because the test function is not using my mocked DataService ? But the answer accepted in this question says that it should work. Now I'm confused ...
I want to know how to [[SomeClazz alloc] init] to return a mocking instance with OCMock?
ios objective-c unit-testing ocmock
Leem.fin
source share