iOS unit test: How to set/update/examine firstResponder?

Jon Reid picture Jon Reid · May 21, 2011 · Viewed 11.2k times · Source

How do you write first responder unit tests?

I'm trying to write a test to confirm that a method advances focus to the next text field. controller is a descendant of UIViewController. But this exploratory test fails:

- (void)testFirstResponder
{
    [controller view];
    [[controller firstTextField] becomeFirstResponder];

    STAssertTrue([[controller firstTextField] isFirstResponder], nil);
}

The first line causes the view to be loaded so that its outlets are in place. The text fields are non-nil. But the test never passes.

I'm guessing that becomeFirstResponder doesn't set the first responder right away, but schedules it for later. So is there a good way to write a unit test against it?

Pulling up answer from comment in accepted answer… Let things run for a short time:

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];

I've also found I need to create a UIWindow and drop the view controller's view into it, as stated in the most-upvoted answer.

Answer

JRG-Developer picture JRG-Developer · Jun 11, 2014

Using Xcode 5.1 and XCTestCase, this seems to work okay:

- (void)testFirstResponder
{      
  // Make sure the controller's view has a window
  UIWindow *window = [[UIWindow alloc] init];
  [window addSubview:controller.view];

  // Call whatever method you're testing
  [controller.textView becomeFirstResponder];

  // Assert that the desired subview is the first responder
  XCTAssertTrue([sut.textView isFirstResponder]);
}

In order for a view/subview to become first responder, it must be part of a view hierarchy, meaning that its root view's window property must be set.

Jon and Sergio mention that you may need to call [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] after calling becomeFirstResponderon your desired subview, but I found that this wasn't required in our instance.

However, your mileage may vary (even depending on the version of Xcode you're using), so you may or may not need to include such call.