I'm writing unit tests for an application and want to check if a UIAlertController
is presented in a specific scenario.
-(void)testBadLogin {
// enter username and password in UITextFields
self.viewController.usernameField.text = @"[email protected]";
self.viewController.passwordField.text = @"incorrect_pass";
[loginButton sendActionsForControlEvents: UIControlEventTouchUpInside];
// this isn't right
XCTAssertNotNil([self.viewController alertController], @"alertController should appear");
}
How do I check if a UIAlertController
has been presented on top of the current view?
"XCTest is not meant to be used to test UI components." is not truly accurate. I am using XCTest for almost every UI testing and it works just fine. The correct answer shall be "Mocking".
I would use OCMock for mocking the tested view controller and "verify" that the method presentViewController... is called with the alert controller. This is a neat solution and works just fine. (You can even ignore that the alert controller is passed to this method and just test that the view controller has been passed the method presentViewController...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With