2015-01-04 5 views
1

나는 Graham Lee가 저술 한 "Test-Driven iOS Development"를 따르고 있으며,이 섹션에서는 전혀 설명되지 않았습니다. 이 아이디어는 을 didFinishLaunchingWithOptions에서 인스턴스화하는 것이 아니라 IBOutlet을 사용하여 UIWindow xib 파일에 연결하는 것입니다. 나는 그 일을 할 수없고 인터넷에서 어떤 예를 찾을 수 없다.어떻게 단위 테스트 didFinishLaunchingWithOptions?

-(void)testWindowHasRootNavigationControllerAfterApplicationLaunch 
{ 
    XCTAssertEqualObjects(window.rootViewController, navigationController, @"App delegate's navigation controller should be the root VC"); 
} 

@implementation iTagNewsAppDelegateTests 
{ 
    UIWindow *window; 
    UINavigationController *navigationController; 
    AppDelegate *appDelegate; 
} 

- (void)setUp { 
    window = [UIWindow new]; 
    navigationController = [UINavigationController new]; 
    appDelegate = [AppDelegate new]; 
    appDelegate.window = window; 
    appDelegate.navigationController = navigationController; 
} 

코드 :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
BrowseOverflowViewController *firstViewController = 
[[BrowseOverflowViewController alloc] initWithNibName: nil bundle: nil]; 
    TopicTableDataSource *dataSource = [[TopicTableDataSource alloc] 
        init]; 
    [dataSource setTopics: [self topics]]; 
    firstViewController.dataSource = dataSource; 
    self.navigationController.viewControllers = 
        [NSArray arrayWithObject: firstViewController]; 
    self.window.rootViewController = self.navigationController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

@interface BrowseOverflowAppDelegate : NSObject <UIApplicationDelegate> 
@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 
@end 

그의 전체 프로젝트가 GitHub. 에 사용자 정의 UIWindow를 정의하는 방법을 어떤 튜토리얼이 있습니까? 많은 감사

답변

0

나는이 책을 읽지 않았지만 AppDelegate의 전체 인스턴스를 테스트 할 수 있다는 것을 알았습니다. 코드에 적응 :

- (void) setUp { 
    //Could also use [[UIApplication sharedApplication] delegate] but I'm worried state may persist 
    iTagNewsAppDelegate* appDelegate = [[iTagNewsAppDelegate alloc] init] 
    [appDelegate application:[UIApplication sharedApplication] didFinishLaunchingWithOptions:nil]; //Couldn't find a better option than sharedApplication here, fine if application param isn't used? 
    //the rest of your setup here 
} 

이 내 프로젝트에서 작동하지만 sharedApplication을 사용의 부작용에 대해 확실하지 않다. 단위 테스트에 알려진 시작 상태가 있고 테스트를 통해 실행중인 앱을 재사용하는 것은 대개 나쁜 일입니다.

관련 문제