I have the below code to test a data task in XCTest. The problem is that I want the line
[saveUserProfile : jsonObject];
to be executed and completed before the next unit test case fires. This is because a value returned from this data call is needed in the next unit case.
Is there anyway to achieve this ?
Thanks !
- (void)testA1DataUserProfile {
XCTestExpectation *expectation = [self expectationWithDescription:@"asynchronous request"];
NSString * newURL = kuserProfileURL;
Environment * activeEnvironment = [Globals activeEnvironment];
NSString * orbitBaseString = activeEnvironment.baseURL;
NSString * fullURLString = [NSString stringWithFormat:@"%@%@%@", orbitBaseString, newURL, kuserName];
NSURL * fullURL = [NSURL URLWithString:fullURLString];
NSLog(@"Full url is %@", fullURL);
NSURLSessionTask *task = [self.session dataTaskWithURL:fullURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
XCTAssertNil(error, @"dataTaskWithURL error %@", error);
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *) response statusCode];
XCTAssertEqual(statusCode, 200, @"status code was not 200; was %ld", (long)statusCode);
}
XCTAssert(data, @"data nil");
//NSLog (@"Data %@", data);
NSError *jsonError;
jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
//(@"JSON Object is %@", jsonObject);
[expectation fulfill];
}];
[task resume];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
//Grab event ID
[self saveUserProfile : jsonObject];
}
Aucun commentaire:
Enregistrer un commentaire