IOS unit test

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值