Search code examples
command-lineios4uiapplicationdelegatexunitgh-unit

How can I use UIApplicationDelegate in command line GHUnit?


I have differences about GHUnit results between run from simulator and run from command line. How can I use UIApplicationDelegate in cli?

In my sample app - tags stackoverflow-6479906. I want to transfer apple's OCUnit example to GHUnit one.

Expect:

Both "appDelegate" is not nil.

Current:

Run from simulator works fine. However run from cli raises exception.https://gist.github.com/1046753

Refs: apple's unittest example

Note: I read async NSURLConnection sample, but I do not find solution and adjust in my UIApplicationDelegate case.


Solution

  • I recommend you design your test case for your app delegate such that it instantiates an instance of it and calls methods and tests outputs from there.

    Consider a structure such as this:

    @interface  FizzBuzzObjCStudyTest : GHTestCase {
       ObjCStudyAppDelegate *appDelegate;
    }
    @end
    
    @implementation FizzBuzzObjCStudyTest
    -(void)setUp
    {
       appDelegate = [[ObjCStudyAppDelegate alloc] init];
    }
    
    -(void)tearDown
    {
       [appDelegate release];
    }
    
    -(void) testAppDelegate
    {
       GHAssertNotNil(appDelegate, @"Cannot find the application delegate", nil);
    
       // test other methods of ObjCStudyAppDelegate here, or make more test methods.
    }
    @end
    

    The GHUnit framework bypasses the UI framework that creates the UIApplicationDelegate in the CLI version. In fact, the GUI version of GHUnit is actually instantiating its own UIApplicationDelegate called GHUnitIPhoneAppDelegate.

    Here is a snippet from GHUnitIOSMain.m showing how it sets up its own app delegate for the GUI version:

    // If GHUNIT_CLI is set we are using the command line interface and run the tests
    // Otherwise load the GUI app
    if (getenv("GHUNIT_CLI")) {
        retVal = [GHTestRunner run];
    } else {
        retVal = UIApplicationMain(argc, argv, nil, @"GHUnitIPhoneAppDelegate");
    }