iOS单元测试:如何单元测试firstResponderStatus

时间:2015-08-12 05:26:06

标签: ios unit-testing xctest first-responder

我正在编写一个测试来确认方法是否将焦点转移到下一个textField。我尝试了iOS unit test: How to set/update/examine firstResponder?的代码。但它失败了。

- (void)testFocusMovedToYearField {
  UIWindow *window = [[UIWindow alloc] init];
  [window addSubview:self.dateCell.txtDay];
  [window addSubview:self.dateCell.txtYear];   
  [self.dateCell completionBlockHelper];
  [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]]; 
  XCTAssertTrue([self.kioskDateCell.txtYear isFirstResponder]);
}

函数completionBlockHelper包含使txtYear成为第一响应者的代码。

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:1)

窗口需要成为关键窗口。在Swift中:window.makeKeyAndVisible()