【问题标题】:Asynchronous unit test not being called by SenTestCaseSenTestCase 未调用异步单元测试
【发布时间】:2012-04-28 08:51:33
【问题描述】:

当我运行单元测试时,我没有到达断点,并且我认为测试没有正确运行我的代码。

我正在尝试进行异步测试,这是我的困难之一。

如何设置测试以便调用 testLookupBook,因为我希望这会导致其他所有内容都被正确调用。

我猜测要测试的函数应该在头文件中的某个位置,但我确实以test 开始我的测试方法。

这是我的头文件:

@interface jabBookScanTest : SenTestCase {
    jabBookScan *instance;
    NSArray *searchResult;
}
@end

测试的实现在这里:

- (void)MethodNameToCallBack:(jabBookScan *)manager 
            resultFromServer:(NSArray *)s {
    searchResult = s;
}

- (BOOL)waitForCompletion:(NSTimeInterval)timeoutSecs {
    NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeoutSecs];
    BOOL done = false;
    do {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate];
        if([timeoutDate timeIntervalSinceNow] < 0.0)
            break;
    } while (!done);

    return done;
}

- (void)testLookupBook {
   NSDate *fiveSecondsFromNow = [NSDate dateWithTimeIntervalSinceNow:5.0];
   [instance lookupBook:(NSString *)@"9780262140874"];
   [[NSRunLoop currentRunLoop] runUntilDate:fiveSecondsFromNow];
   STAssertTrue([self waitForCompletion:90.0], @"Failed to get any results in time");
}

【问题讨论】:

    标签: objective-c ios5 asynchronous sentestingkit


    【解决方案1】:

    我愿意这样做

    - (void)testLookupBook {
       NSDate *fiveSecondsFromNow = [NSDate dateWithTimeIntervalSinceNow:5.0];
       [instance lookupBook:(NSString *)@"9780262140874"];
       WAIT_WHILE(searchResult == nil, 1.0); 
    }
    

    WAIT_WHILE(expressionIsTrue, seconds)-macro 将评估输入,直到表达式不正确或达到时间限制。下面是使用 NSRunLoop 模式。你可以在这里找到项目:

    https://github.com/hfossli/AGWaitForAsyncTestHelper

    【讨论】:

      【解决方案2】:

      我忘记将方法添加到我的头文件中。

      @interface jabBookScanTest : SenTestCase<jabBookScanDelegate> {
          jabBookScan *instance;
          NSArray *searchResult;
      }
      - (void) testLookupBook;
      
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        • 2017-06-01
        • 1970-01-01
        • 2012-07-15
        • 2021-10-13
        相关资源
        最近更新 更多