iOS单元测试 - 同时使用NSRunLoop和AFNetworking时出现错误的访问错误

时间:2014-07-11 19:11:29

标签: unit-testing ios7 afnetworking nsrunloop

我正在使用下面的代码在我的iOS7单元测试中使用AFNetworking测试异步调用。当我单独运行测试,甚至整个测试用例时,代码运行正常。但是,当我运行整个测试套件时,我在行[theRL runMode:NSDefaultRunLoopMode beforeDate:date];

上获得EXC_BAD_ACCESS(代码= 1,地址0x37a)
  NSRunLoop *theRL = [NSRunLoop currentRunLoop];

    //setup timeout
    float waitIncrement = 0.1f;
    int timeoutCounter  = (int)(60 / waitIncrement); //30 sec timeout
    BOOL controlConditionReached = NO;


    // Begin a run loop terminated when the downloadComplete it set to true
    while (controlConditionReached == NO)
    {
        NSDate* date = [NSDate dateWithTimeIntervalSinceNow:waitIncrement];

        //Here: EXC_BAD_ACCESS (code=1, address0x7a) when running the entire test suite
        [theRL runMode:NSDefaultRunLoopMode beforeDate:date];
        //control condition is set in one of your async operation delegate methods or blocks
        controlConditionReached = self.downloadComplete || self.downloadFailed||self.cachedVideoAvailable ;

        //if there's no response - timeout after some time
        if(--timeoutCounter <= 0)
        {
            break;
        }
    }

我看到,在我使用循环的同时,AFNetoworking正在使用这种方法:

+ (void)networkRequestThreadEntryPoint:(id)__unused object {
    @autoreleasepool {
        [[NSThread currentThread] setName:@"AFNetworking"];

        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [runLoop run];
    }
}

如何识别导致此错误的原因?如果我将方法的运行循环模式更改为NSRunLoopCommonModes,那么我的所有测试都会失败

0 个答案:

没有答案