Here is a basic test case to test this functionality. You should replace UIViewController
with your VC class. It is also not recommended to call -viewWillAppear:
directly, but in the case of this specific unit test it could be alright.
-(void)testKeyboardShown
{
UIViewController* controller = [[UIViewController alloc] init];
[controller viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillShowNotification object:nil];
XCTAssertEqual(controller.view.frame.origin.y, -kOFFSET_FOR_KEYBOARD, "View should move up");
[[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillHideNotification object:nil];
XCTAssertEqual(controller.view.frame.origin.y, 0, "View should move down");
}
Bonus: The userInfo dictionary of UIKeyboardWillShowNotification includes a property that tells you the height of the keyboard; you could use this value instead of hardcoding your own offset. It also includes values for animation duration and timing curve, so your animation could more correctly follow that of the keyboard rather than hardcoding 0.3 seconds.
EDIT
To test the dynamic keyboard height, you would need to pass a userInfo dictionary with the UIKeyboardWillShowNotification that includes a fake frame for the keyboard:
CGRect keyboardFrame = CGRectMake(0, 0, 0, 20);
[[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillShowNotification object:nil userInfo:@{ UIKeyboardFrameBeginUserInfoKey : [NSValue valueWithCGRect:keyboardFrame] }];
XCTAssertEqual(controller.view.frame.origin.y, -keyboardFrame.size.height, "View should move up");