Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have Asynchronous Procedures (e.x., login) in "setUp" method Xcode 6 for Unit Testing

I'm looking for something that uses XCTextExpectation in the setup or teardown methods. In Objective-C, it would look something like this:

- (void)setUp {

XCTestExpectation *getUserAsyncComplete = [self expectationWithDescription:@"Get Request Attempted and Finished"];

[Connection loginWithEmail:@"[email protected]" onSuccess:^void(^) { 
     [getUserAsyncComplete fulfill];
} onError:nil;

[self waitForExpectationsWithTimeout:[self.timeLimit doubleValue] handler:^(NSError *error) {
    if (error != nil) {
        XCTFail(@"Failure: user retrieval exceeded %f seconds.", [self.timeLimit doubleValue]);
    }
}];
}

I've tried this code and it doesn't seem to be working; either that could be because Xcode 6 is still in beta, or it's not supported. Even if there is a solution in Swift, that would be greatly appreciated.

like image 688
Mihir Avatar asked Jun 22 '14 03:06

Mihir


1 Answers

You mistyped the method name, this seems to work just fine (Xcode 6, beta 2)

- (void)setUp {
    [super setUp];

    XCTestExpectation *exp = [self expectationWithDescription:@"Login"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(29 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [exp fulfill];
    });

    [self waitForExpectationsWithTimeout:30 handler:^(NSError *error) {
        // handle failure
    }];
}

- (void)testExample {
    XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
}
like image 107
Sash Zats Avatar answered Oct 18 '22 18:10

Sash Zats