我最近采用了iOS企业应用程序,充当媒体CMS的客户端。换句话说,Web应用程序管理视频目录,图像幻灯片放映和静态/离线HTML文件。 iOS应用程序连接到Web应用程序并将此内容下载到自身以供离线使用。
热点是iOS和Web应用程序之间的同步逻辑。有没有建议对这两个项目之间的逻辑进行单元测试?
答案 0 :(得分:1)
NSRunLoop
的{{1}}方法,这是我的方法。
基本上,为了让您的网络代码有机会与网络进行通信,您希望将您的单元测试置于一种繁忙的等待状态,实际上让每个人都能做一些工作,但您也想“回头看”在你的单元测试中定期检查你的操作是否完成。
这是我的“去做工作”方法:
runUntilDate:
所以,在每个单元测试中:
-(BOOL)runLooperDooper:(NSTimeInterval)timeoutInSeconds{
NSDate* giveUpDate = [NSDate dateWithTimeIntervalSinceNow:timeoutInSeconds];
// loop until the operation completes and sets stopRunLoop = TRUE
// or until the timeout has expired
// stopRunLoop is a instance variable of the UnitTest object... take care to reset at the start or end of each test!
while (!stopRunLoop && [giveUpDate timeIntervalSinceNow] > 0)
{
// run the current run loop for 1.0 second(s) to give the operation code a chance to work
NSDate *stopDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
[[NSRunLoop currentRunLoop] runUntilDate:stopDate];
}
return stopRunLoop;
}
然后,我还为我的网络代码注册了一些事件处理程序,通过将 NSTimeInterval testSpecificTimeout = 60; //60 seconds or however long you need...
[self runLooperDooper:testSpecificTimeout];
STAssertTrue(stopRunLoop, @"Failed to complete before runloop expired after %f seconds", timeoutInSeconds);
ivar设置为stopRunLoop
来了解其操作何时完成。