I'm trying to set up my unit test to use the latest facebook SDK and am running into issues...
Here is the code i have:
[FBSession setDefaultAppID: @"323351877676429"];
FBTestSession *session = [FBTestSession sessionWithSharedUserWithPermissions: [NSArray array]];
STAssertNotNil(session, @"could not create test session");
[FBTestSession openActiveSessionWithReadPermissions:@[]
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
NSLog(@"completed");
[FBSession setActiveSession: session];
STAssertNil(error, error.description);
STAssertNotNil(FBSession.activeSession, @"FBSession missing");
STAssertNotNil(FBSession.activeSession.accessTokenData, @"FBSession missing");
STAssertNotNil(FBSession.activeSession.accessTokenData.accessToken, @"facebook token is nil");
STAssertNotNil(session, @"FBSession missing");
STAssertNotNil(session.accessTokenData, @"FBSession missing");
STAssertNotNil(session.accessTokenData.accessToken, @"facebook token is nil");
userModel *userM = [[userModel alloc] init];
[userM awakeFromNib];
} ];
This works well except that as it runs asynchronously, when my test are run the session is not ready and thus i have session.accessTokenData == nil., which make my other test fail.
If i add the below code to my set up method after the openSession call, then the setup method never returns.
while (!FBSession.activeSession.accessTokenData)
[NSThread sleepForTimeInterval:.2];
Is there an exemple of proper use of FBTestSession somewhere? Any clue as to how to proceed?
Thanks, olivier
You are close, you just need to wait until the blocks are complete. Here is a working example (slightly shortened/modified) that you can use for reference.
- (void)testFacebookLogin {
__block bool blockFinished = NO;
FBTestSession *fbSession = [FBTestSession sessionWithSharedUserWithPermissions:@[@"email"]];
[fbSession openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
NSLog(@"session %@, status %d, error %@", session, status, error);
[FBSession setActiveSession:session];
FBRequest *me = [FBRequest requestForMe];
NSLog(@"me request %@", me);
[me startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *my,
NSError *error) {
STAssertNotNil(my.id, @"id shouldn't be nil");
blockFinished = YES;
}];
}];
// Run loop
NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:10];
while (blockFinished == NO && [loopUntil timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:loopUntil];
}
}