您不需要嘲笑它,这种目标/行动方法感觉就像是一种真正迂回的方式来获得您想要的东西。只要您的控制器是UITextField
的委托,textFieldShouldReturn:
就会在点击 Return(或在本例中为 Done)键时触发。
#pragma mark - UITextFieldDelegate
-(void)textFieldShouldReturn:(UITextField *)textField {
if (someConditionThatDeterminesWhetherKeyboardShouldCollapse) {
[textField resignFirstResponder];
}
}
textFieldShouldReturn:
然后,您可以在满足和不满足条件时测试对控制器的调用。如果需要,您还可以通过调用确保您的视图已加载到测试用例中loadView
,然后断言文本字段的委托是您的控制器,这将确保视图正确连接。
-(void)testDoneShouldCollapseKeyboard {
[controller loadView];
expect(controller.textField.delegate).to.beIdenticalTo(controller);
id mockTextField = [OCMockObject partialMockForObject:controller.textField];
[[mockTextField expect] resignFirstResponder];
controller.someConditionThatDeterminesWhetherKeyboardShouldCollapse = YES;
[controller textFieldShouldReturn:controller.textField];
[mockTextField verify];
[[mockTextField reject] resignFirstResponder];
controller.someConditionThatDeterminesWhetherKeyboardShouldCollapse = NO;
[controller textFieldShouldReturn:controller.textField];
[mockTextField verify];
}