Created
November 20, 2010 14:44
-
-
Save vl4dimir/707869 to your computer and use it in GitHub Desktop.
Example asynchronous GHUnit test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <SenTestingKit/SenTestingKit.h> | |
@interface AsyncTests : SenTestCase { | |
NSCondition* condition; | |
BOOL operationSucceeded; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "AsyncTests.h" | |
@implementation AsyncTests | |
- (void) setUp | |
{ | |
condition = [[NSCondition alloc] init]; | |
operationSucceeded = NO; | |
} | |
- (void) tearDown | |
{ | |
[condition release]; | |
} | |
- (void) testAsynchronousMethodCall | |
{ | |
// Create and start an asynchronous operation | |
NSOperationQueue* queue = [[[NSOperationQueue alloc] init] autorelease]; | |
MyOperation* operation = [[[MyOperation alloc] init] autorelease]; | |
[queue addOperation:operation]; | |
// Block and wait for one of the delegate methods to be called | |
[condition lock]; | |
[condition wait]; | |
[condition unlock]; | |
// Our operation has completed at this point, so we can check if it was successful | |
STAssertTrue(operationSucceeded, @""); | |
} | |
- (void) myOperationDidFinish | |
{ | |
operationSucceeded = YES; | |
[condition lock]; | |
[condition signal]; | |
[condition unlock]; | |
} | |
- (void) myOperationDidFailWithError:(NSError*)error | |
{ | |
operationSucceeded = NO; | |
[condition lock]; | |
[condition signal]; | |
[condition unlock]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OCUnit / SenTesting framework does not run within the normal RunLoop; which NSURLConnection relies on to execute. This is why the request never starts and none of the delegate methods are ever called. I tried to make this work in OCUnit for some time (including manually 'spinning' the RunLoop) with no success. The simplest/best alternative is using another test framework which does run tests in full application mode. I recommend GHUnit for ease of use; it's quick to set up and there are plenty of examples online.