0

I want to test my app delegate make window as key window after launch. So I write the following tests.

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

In my app delegate the method applicaton:didFinishLaunchingWithOptions:

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

The test failed and told me window.keyWindow should be true. Is there anything wrong? How can I fix the test?

newguy
  • 5,668
  • 12
  • 55
  • 95

1 Answers1

2

I imagine this is a similar problem to my question iOS unit test: How to set/update/examine firstResponder? The actual activating of the key window is probably something that happens in the main run loop. To give it a chance to run, try calling this in your test:

- (void)runForShortTime
{
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];
}
Community
  • 1
  • 1
Jon Reid
  • 20,545
  • 2
  • 64
  • 95
  • Thank you for your reply. I was actually watching your video on unit testing. Back to the question, where do you suggest me put the method? I am a bit confused about how this would affect the test. – newguy Aug 30 '13 at 05:16
  • Call `-runForShortTime` before you test for key window. – Jon Reid Aug 30 '13 at 15:06