2

I just setup application tests with OCUnit for my iOS project and am trying to figure out what to do with authentication. My app has a forced login screen where the user is required to authenticate with Facebook prior to entering the main app. When the user decides to authenticate, it pops them into Safari, takes them through an authentication sequence that is managed by Facebook, and then brings them back into the app (with further registration steps if they are a new user).

What I'm trying to figure out is, how can I get an application test to get past the login screen?

One option I've considered is making a new build configuration (e.g. "Test") that adds a preprocessor macro (e.g. "TEST = 1") that I can detect in my code. Then, only for that test configuration, I could replace my regular login code with a FBTestSession that wouldn't require the app to pop out to Safari. However, it would be nice if there were a cleaner option...

Here's the FBTestSession documentation: https://developers.facebook.com/docs/reference/ios/3.0/class/FBTestSession/

Thanks in advance!

Rahul Jaswa
  • 509
  • 4
  • 17

1 Answers1

2

I can think of two ways, both involving runtime detection of whether the testing framework is present.

In your login code, see if you're running tests. And if you are, don't continue:

BOOL runningTests = NSClassFromString(@"SenTestCase") != nil;
if (runningTests)
    return;

Another way is to use a different app delegate for tests that does almost nothing. In main.m:

@autoreleasepool {
    BOOL runningTests = NSClassFromString(@"SenTestCase") != nil;
    Class delegateClass = runningTests ? [TestingAppDelegate class] : [AppDelegate class];
    return UIApplicationMain(argc, argv, nil, NSStringFromClass(delegateClass));
}

The advantage to the first method is that it's quick & easy. The advantage to the second method is that it's thorough, giving you complete control over your app during testing.

Jon Reid
  • 20,545
  • 2
  • 64
  • 95