Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT (2-18-2014): I just stumbled across this article with a more elegant solution.</p> <p><a href="http://www.infinite-loop.dk/blog/2011/04/unittesting-asynchronous-network-access/">http://www.infinite-loop.dk/blog/2011/04/unittesting-asynchronous-network-access/</a></p> <p>Essentially, you have the following method:</p> <pre><code>- (BOOL)waitForCompletion:(NSTimeInterval)timeoutSecs { NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeoutSecs]; do { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate]; if([timeoutDate timeIntervalSinceNow] &lt; 0.0) break; } while (!done); return done; } </code></pre> <p>At the end of your test method, you make sure things haven't timed out:</p> <pre><code>STAssertTrue([self waitForCompletion:5.0], @"Timeout"); </code></pre> <p>Basic format:</p> <pre><code>- (void)testAsync { // 1. Call method which executes something asynchronously [obj doAsyncOnSuccess:^(id result) { STAssertNotNil(result); done = YES; } onError:^(NSError *error) [ STFail(); done = YES; } // 2. Determine timeout STAssertTrue([self waitForCompletion:5.0], @"Timeout"); } </code></pre> <p>==============</p> <p>I'm late to the party, but I came across a very simple solution. (Many thanks to <a href="http://www.cocoabuilder.com/archive/xcode/247124-asynchronous-unit-testing.html">http://www.cocoabuilder.com/archive/xcode/247124-asynchronous-unit-testing.html</a>)</p> <p>.h file:</p> <pre><code>@property (nonatomic) BOOL isDone; </code></pre> <p>.m file:</p> <pre><code>- (void)testAsynchronousMethod { // 1. call method which executes something asynchronously. // 2. let the run loop do its thing and wait until self.isDone == YES self.isDone = NO; NSDate *untilDate; while (!self.isDone) { untilDate = [NSDate dateWithTimeIntervalSinceNow:1.0] [[NSRunLoop currentRunLoop] runUntilDate:untilDate]; NSLog(@"Polling..."); } // 3. test what you want to test } </code></pre> <p><code>isDone</code> is set to <code>YES</code> in the thread that the asynchronous method is executing. </p> <p>So in this case, I created and started the NSURLConnection at step 1 and made the delegate of it this test class. In </p> <p><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response</code></p> <p>I set <code>self.isDone = YES;</code>. We break out of the while loop and the test is executed. Done.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload