objective-c - Xcode 4 : Asynchronous testing? 中的 SenTestingKit

标签 objective-c unit-testing ocunit

我一直在寻找一种方法来使用 SenTestingKit 在我的客户端代码和我们的服务器之间进行一些集成测试。我没有任何运气。似乎一旦代码在方法中运行,对象就会被销毁。这意味着任何异步响应都不会调用选择器。

问题:

  1. 有没有办法让对象保持实例化,直到我认为适合销毁它的时候 - 即。测试完成后?
  2. 如果没有,我如何创建一个阻塞(即同步操作)直到测试完成的类?

仅供引用,我正在运行一个我知道预期结果的测试服务器。

我已经进行了相当多的谷歌搜索,但还没有看到任何关于这一点的证据。我相信其他人也会感兴趣。

最佳答案

您可以使用信号量等待异步方法完成。

- (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/

关于objective-c - Xcode 4 : Asynchronous testing? 中的 SenTestingKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6190486/

相关文章:

iphone - 多次添加同一个 subview 进行查看

ios - Objective C 将图像保存到图库

unit-testing - 何时以及为什么应该编写参数化的 JUnit 测试用例?

python - 如何在 Python 中模拟用户输入

ios - 如何为 cocos2d 应用设置单元测试?

ios - 2 UIViewController中的UITableView相互影响

ios - RestKit 中发送两个请求然后仅在两个请求返回后才执行操作的最佳方法是什么?

unit-testing - Lua API 单元测试

objective-c - OCUnit 中的简化断言

objective-c - 对 UITextField 上的操作进行单元测试