Unit tests with designated AppDelegate crashes on XCode 7

886 Views Asked by At

My unit test target crashes on Xcode 7.

I perform unit tests on my project with a designated app delegate class, different from my regular app delegate . The UnitTestAppDelegate is not a member of the app target, only of the test target.

In Xcode 6 it is working, the correct delegate is used, But in Xcode 7 (beta 6) the app crashes when trying to load to test delegate. I test using simulators, both iOS 8 and 9.

The file is not known to the app target:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to instantiate the UIApplication delegate instance. No class named UnitTestAppDelegate is loaded.'

My main.m file looks like this:

int main(int argc, char* argv[])
{
    int returnValue;   

    @autoreleasepool
    {
        if (NSClassFromString(@"XCTest") != nil)
        {
            //use the test delegate
            returnValue = UIApplicationMain(argc, argv, nil, @"UnitTestAppDelegate");
        }
        else
        {
            //use the normal delegate
            returnValue = UIApplicationMain(argc, argv, nil, @"AppDelegate");
        }
    }

    return returnValue;
}

I'm also attaching a screen shot of how my scheme is configured (the run and test are in the same scheme, although I also tried separating them).

enter image description here

Adding my UnitTestAppDelegate as a member of the app target fixes the crash, but I obviously don't want to add the file to my app.

Anyone else encountered this?

Any ideas?

1

There are 1 best solutions below

1
On

See this related comment about the same issue as seen in Xcode 7 beta 6: http://qualitycoding.org/app-delegate-for-tests/#comment-63240