in my fixture setUp i have the following
-(void)setUp{
_vc = [OCMockObject partialMockForObject:[[InboxViewController alloc] init]];
//stub out the view stuff
[[_vc stub] removeTask:OCMOCK_ANY];
[[_vc stub] insertTask:OCMOCK_ANY];
}
There are 15 tests in the fixture, however, I need to actually test that those 2 methods are invoked, so I wrote 2 tests
-(void)someTest{
[[_vc expect] removeTask:OCMOCK_ANY];
[_vc removeAllTasksFromList:taskList notInList:newTaskList];
[_vc verify];
}
but that test fails
i also tried
-(void)someTest{
[[_vc stopMocking];
[[_vc expect] removeTask:OCMOCK_ANY];
[[_vc stub] removeTask:OCMOCK_ANY];
[_vc removeAllTasksFromList:taskList notInList:newTaskList];
[_vc verify];
}
But the test still fails. Am I missing something, or is this just how OCMock works?
The only way I can make it work is like this
-(void)someTest{
//re create and init the mock object
_vc = [OCMockObject partialMockForObject:[[InboxViewController alloc] init]];
[[_vc expect] removeTask:OCMOCK_ANY];
[[_vc stub] removeTask:OCMOCK_ANY];
[_vc removeAllTasksFromList:taskList notInList:newTaskList];
[_vc verify];
}
Maybe the documentation should be clearer. What stopMocking does for a partial mock is to restore the real object, in your case the InboxViewController, to its original state. Calling stopMocking does not reset the mock object, which means it does not clear the stubs and expectations. You can always call stopMocking and then create a new mock for the same real object.
As was pointed out in another answer, stubbing and expecting the same method is generally better avoided, but if you have to do it, make sure to set up the expect before the stub; otherwise the stub will handle the invocations and the expect will never see them.
I know that traditionally many people recommend to use the setup method to set up the test subject. My personal experience, over the years, is that it's generally not worth it. Saving a couple of lines in each test might look attractive but in the end it does create coupling between the individual tests, making the suite more brittle.