SenTestingKit in Xcode 4: Asynchronous testing?

前端 未结 5 1623
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 23:29

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 t

相关标签:
5条回答
  • 2020-12-12 23:35

    Kiwi supports asynchronous testing. Kiwi is a Behavior Driven Development (BDD) library for iOS that extends SentTestingKit (OCUnit), so it's easy to set up & use.

    Also, check out:

    • iOS Tests/Specs TDD/BDD and Integration & Acceptance Testing.
    • Testing asynchronous code on iOS with OCunit
    0 讨论(0)
  • 2020-12-12 23:39

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

    0 讨论(0)
  • 2020-12-12 23:50

    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/

    0 讨论(0)
  • 2020-12-12 23:51

    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.
    0 讨论(0)
  • 2020-12-12 23:54

    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");
    }
    
    0 讨论(0)
提交回复
热议问题