1

I've recently adopted an iOS corporate app that acts as a client to a media CMS. Put another way, the web app manages a catalog of videos, image slide shows, and static/offline HTML files. The iOS app connects to the web app and downloads this content to itself for offline usage.

The hot spot is the synchronization logic between the iOS and web applications. Is there any advice for unit-testing the logic between these two projects?

4

1 回答 1

1

是的,你必须“欺骗”一些人。一种方法是使用NSRunLoop'srunUntilDate:方法,这是我的方法。

基本上,为了让您的 Web 代码有机会与 Web 通信,您希望将您的单元测试置于一种忙碌等待状态,实际上让每个人都可以做一些工作,但您还想在您的单元中“检查” - 定期测试以查看您的操作是否完成。

这是我的“去工作”方法:

-(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);

然后,我还为我的 Web 代码注册了一些事件处理程序,以便通过将stopRunLoopivar 设置为TRUE.

于 2012-08-07T17:25:00.663 回答