使用OCUnit对keyWindow进行单元测试会引发错误

时间:2013-08-30 00:14:13

标签: ios unit-testing ocunit

我希望在启动后测试我的app delegate make窗口作为关键窗口。所以我写下面的测试。

- (void)setUp
{
    window = [[UIWindow alloc] init];
    appDelegate = [[FGAppDelegate alloc] init];
    appDelegate.window = window;
    appDidFinishLaunchingReturn = [appDelegate application: nil didFinishLaunchingWithOptions:nil];
}

- (void)tearDown
{
    window = nil;
    appDelegate = nil;
}
- (void)testWindowIsKeyAfterApplicationLaunch
{
    STAssertTrue(window.keyWindow, @"App delegate's window should be key.");
}

在我的应用中委托方法应用:didFinishLaunchingWithOptions:

  ...
  self.window.rootViewController = self.tabBarController;
  [self.window makeKeyAndVisible];
  return YES;
}

测试失败并告诉我window.keyWindow应该是真的。有什么不对的吗?我该如何修复测试?

1 个答案:

答案 0 :(得分:2)

我想这与我的问题类似的问题iOS unit test: How to set/update/examine firstResponder?关键窗口的实际激活可能是在主运行循环中发生的事情。为了让它有机会运行,请尝试在测试中调用它:

- (void)runForShortTime
{
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];
}