Xcode 4中的SenTestingKit:异步测试?

| 我一直在寻找一种使用SenTestingKit在客户端代码和服务器之间进行集成测试的方法。我没有运气。看起来,一旦代码在方法中运行,对象就会被破坏。这意味着任何异步响应都不会调用选择器。 问题: 有没有一种方法可以保持对象实例化,直到我认为合适的时候可以销毁它-即。测试完成后? 如果没有,我如何创建一个阻塞(即同步执行)直到测试完成的类? 仅供参考,我正在运行测试服务器,我知道预期的结果。 我已经做了很多谷歌搜索,但是还没有看到关于此的一种或另一种证明。我确定其他人也会对此感兴趣。     
已邀请:
        两种选择: 切换到GHUnit,它实际上包含等待异步事件的支持 缩小测试的设计范围,以便您可以按现状测试事物。例如。测试您的控制器代码是否会导致选择器分离并运行,并(单独)测试该选择器是否应执行其应做的工作。如果这两种方法都起作用,那么您可以确信控制器可以分离出正确的工作。     
        您可以使用信号量来等待异步方法完成。
- (void)testBlockMethod {
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    // Your block method eg. AFNetworking
    NSURL *url = [NSURL URLWithString:@\"http://httpbin.org/ip\"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@\"IP Address: %@\", [JSON valueForKeyPath:@\"origin\"]);
        STAssertNotNil(JSON, @\"JSON not loaded\");
        // Signal that block has completed
        dispatch_semaphore_signal(semaphore);
    } failure:nil];
    [operation start];

    // Run loop
    while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                 beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
    dispatch_release(semaphore);
} http://samwize.com/2012/10/03/sentestingkit-does-not-support-wait-for-blocks/     
        Kiwi支持异步测试。 Kiwi是iOS的行为驱动开发(BDD)库,它扩展了SentTestingKit(OCUnit),因此易于设置和使用。 另外,请查看: iOS测试/规范TDD / BDD和集成与验收测试。 使用OCunit在iOS上测试异步代码     
        这个项目https://github.com/hfossli/AGAsyncTestHelper有一个非常方便的宏
WAIT_WHILE(<expression_to_evaluate>, <max_duration>);
您可以像这样编写测试
- (void)testDoSomething {

    __block BOOL somethingIsDone = NO;

    [MyObject doSomethingAsyncThenRunCompletionBlockOnMainQueue:^{
        somethingIsDone = YES;
    }];

    WAIT_WHILE(!somethingIsDone, 1.0); 
    NSLog(@\"This won\'t be reached until async job is done\");
}
    
        检出SenTestingKitAsync项目-https://github.com/nxtbgthng/SenTestingKitAsync。相关博客在这里-http://www.objc.io/issue-2/async-testing.html     

要回复问题请先登录注册