1. Focusonly on the logic of the function itself, stub all the dependences from system,not only including the network requesting, file system access, but also forsome system api call, such as Calendar, DateTime and System settings.
2. If yourtest is actually an integration test, you have to do some stuff based on theiOS SDK or some I/O operation. please do ensure your test case may run fast andreliably. Please get rid of sending real web requests to the server and verifythe response.
3. Doasynchronous verification as less as possible. if you have to, please use :
********************************************************************************************
Verify with delay
id mock =OCMStrictClassMock([SomeClass class]);
OCMExpect([mock someMethod]);
/* run code under test, which is assumed to callsomeMethod eventually */
OCMVerifyAllWithDelay(mock, aDelay);
In certain cases the expected method will only be called whenthe run loop is active. For these cases it is possible to delay theverification for a while. Note that aDelay (expressedas NSTimeInterval) is the maximum the mock will wait. Itnormally returns as soon as the expectations have been met.
Note that:
Setting up expect after stub onthe same method does not work
id mock =OCMStrictClassMock([SomeClass class]);
OCMStub([mock someMethod]).andReturn(@"a string");
OCMExpect([mock someMethod]);
/* run code under test */
OCMVerifyAll(mock); // will complain that someMethod has not been called
The code above first sets up a stub for someMethod and afterwards an expectation for the samemethod. Due to the way mock objects are currently implemented any callsto someMethod are handled by the stub. This means that even ifthe method is called the verify fails. It is possible to avoid this problem byadding andReturn to the expect statement. You can also set up astub after the expect.
********************************************************************************************
4. Keep inmind that don’t make your verification out of the scope of your functionlifetime under testing. wait till all your verification pass or failbefore switch to next case.
5. Makesure all your verification run when testing, avoid putting yourverification in a callback that may not be invoked.