Xcode 4 中的 SenTestingKit:异步测试?

发布于 2024-11-10 09:59:31 字数 308 浏览 4 评论 0原文

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

问题:

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

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

我已经做了相当多的谷歌搜索,但还没有看到这样或那样的证据。我相信其他人也会感兴趣。

I have been searching for a way to use SenTestingKit to do some integration testing between my client-side code and our server. I haven't had any luck. It seems that once the code is run in a method, the object gets destroyed. This means that any asynchronous responses never call the selectors.

Questions:

  1. Is there a way to keep the object instantiated until such time as I see fit to destroy it - ie. after the tests have completed?
  2. If not, how could I create a class that blocks (ie. acts synchronously) until the tests are completed?

FYI, I'm running a test server where I know the expected results.

I've done a fair bit of Googling but haven't seen proof one way or another about this. I'm sure others would be interested as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二货你真萌 2024-11-17 09:59:32

这个项目 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");
}

This project https://github.com/hfossli/AGAsyncTestHelper has a very convenient macro

WAIT_WHILE(<expression_to_evaluate>, <max_duration>);

Which ables you to write the test like so

- (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");
}
笨笨の傻瓜 2024-11-17 09:59:32

查看 SenTestingKitAsync 项目 - https://github.com/nxtbgthng/SenTestingKitAsync。相关博客在这里 - http://www.objc.io/issue-2 /async-testing.html

Checkout the SenTestingKitAsync project - https://github.com/nxtbgthng/SenTestingKitAsync. The related blog is here - http://www.objc.io/issue-2/async-testing.html

我家小可爱 2024-11-17 09:59:31

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

- (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-不支持等待块/

You can use a semaphore to wait until the asynchronous method finishes.

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

我不是你的备胎 2024-11-17 09:59:31

两个选择:

  • 切换到 GHUnit,它实际上包含对等待异步事件的支持,
  • 缩小了设计范围您的测试,以便您可以测试事物的现状。例如,测试您的控制器代码是否会导致选择器分离并运行,并(单独)测试该选择器是否执行其应有的操作。如果这两件事都有效,那么您可以确信您的控制器会分离正确的工作。

Two options:

  • switch to GHUnit, which actually contains support for waiting for asynchronous events
  • narrow the design of your tests so that you can test things as they stand. E.g. test that your controller code causes a selector to be detached and run, and (separately) test that this selector does what it ought. If both of those things work, then you can be confident that your controller detaches the correct work.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文