简体   繁体   English

如何对didFinishLaunchingWithOptions进行单元测试?

[英]How to unit test didFinishLaunchingWithOptions?

I am following the book "Test-Driven iOS Development" by Graham Lee and came across this section that isn't well explained at all. 我关注的是格雷厄姆·李(Graham Lee)撰写的《测试驱动的iOS开发》一书,并偶然发现了这一部分,而这一部分并没有得到很好的解释。 The idea is not to instantiate UIWindow in the didFinishLaunchingWithOptions but rather using an IBOutlet and hook it to an UIWindow xib file. 这个想法不是要在didFinishLaunchingWithOptions实例化UIWindow ,而是使用IBOutlet并将其挂钩到UIWindow xib文件。 I can't get that working and can't find any example on the internet. 我无法正常工作,也无法在互联网上找到任何示例。

-(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;
}

Code: 码:

- (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

His full project is on GitHub. 他的完整项目在GitHub上。 Is there any tutorial how to define a custom UIWindow ? 有任何教程如何定义自定义UIWindow吗? many thanks 非常感谢

I haven't read that book but found that I was able to test a full instance of my AppDelegate. 我没有看过那本书,但是发现我能够测试AppDelegate的完整实例。 Adapting it to your code: 使其适应您的代码:

- (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
}

This works in my project but I'm not sure about the side effects of using sharedApplication. 这在我的项目中有效,但是我不确定使用sharedApplication的副作用。 You want a unit test to have a known beginning state and reusing the running app throughout your tests is usually a bad thing. 您希望单元测试具有已知的开始状态,并且在整个测试中重用正在运行的应用程序通常是一件坏事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM