【问题标题】:Unit testing keyWindow using OCUnit throws an error使用 OCUnit 对 keyWindow 进行单元测试会引发错误
【发布时间】:2013-08-30 00:14:13
【问题描述】:

我想在启动后测试我的应用程序委托制作窗口作为关键窗口。所以我写了以下测试。

- (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.");
}

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

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

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

【问题讨论】:

    标签: ios unit-testing ocunit


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。我实际上正在观看您有关单元测试的视频。回到问题,你建议我把方法放在哪里?我对这将如何影响测试感到有些困惑。
    • 在测试关键窗口之前致电-runForShortTime
    猜你喜欢
    • 2019-09-12
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多