[英]How to unit test didFinishLaunchingWithOptions?
我关注的是格雷厄姆·李(Graham Lee)撰写的《测试驱动的iOS开发》一书,并偶然发现了这一部分,而这一部分并没有得到很好的解释。 这个想法不是要在didFinishLaunchingWithOptions
实例化UIWindow
,而是使用IBOutlet并将其挂钩到UIWindow
xib文件。 我无法正常工作,也无法在互联网上找到任何示例。
-(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
吗? 非常感谢
我没有看过那本书,但是发现我能够测试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的副作用。 您希望单元测试具有已知的开始状态,并且在整个测试中重用正在运行的应用程序通常是一件坏事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.