Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if UIAlertController is Presented in an XCTest Case

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?

like image 671
JAL Avatar asked Oct 06 '14 21:10

JAL


1 Answers

"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...)

like image 70
Abdalrahman Shatou Avatar answered Sep 24 '22 02:09

Abdalrahman Shatou