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.