Async testing with xcode and any testing framework such as xctest, sentestingkit, expecta, etc.

I wrote TRVSMonitor, a synchronization construct with the ability to wait until signalled that a condition was met.

This makes asynchronous testing easy. TRVSMonitor works with any testing framework too.

@interface APIClientTests : XCTestCase
@property (nonatomic, strong, readwrite) NSURLSession *URLSession;
@end

@implementation APIClientTests

- (void)setUp {
    self.URLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}

- (void)testAPIEndpoint {
    __block NSDictionary *JSON = nil;
    __block TRVSMonitor *monitor = [TRVSMonitor monitor];

    [self.URLSession dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:8000"]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        [monitor signal];
    }];

    [monitor wait];

    XCTAssert(JSON);
    XCTAssertEqualObjects(@"1", JSON[@"id"]);
    XCTAssertEqualObjects(@"travis jeffery", JSON[@"name"]);
}

@end

Check the readme for the deets.


Follow me on Twitter to keep up with what I’ve learned building my personal finance tool, Stash.

Subscribe to my mailing list to learn more about technology and business.

Your email will never be shared with anyone else.

Read more posts …
Written: October 11th, 2013
Categories: objc, ios, mac, async, testing, xctest