Check if UIAlertController is represented in XCTest Case

I am writing unit tests for an application and want to check if the UIAlertController is UIAlertController in a specific scenario.

 -(void)testBadLogin { // enter username and password in UITextFields self.viewController.usernameField.text = @" test@test.com "; self.viewController.passwordField.text = @"incorrect_pass"; [loginButton sendActionsForControlEvents: UIControlEventTouchUpInside]; // this isn't right XCTAssertNotNil([self.viewController alertController], @"alertController should appear"); } 

How to check if a UIAlertController presented on top of the current view?

+7
source share
5 answers

"XCTest is not intended to test user interface components." not really accurate. I use XCTest for almost every UI test, and it works just fine. The correct answer should be "Mocking".

I would use OCMock to mock the trusted view controller and "verify" that the presentViewController ... method is being called with the warning controller. This is a neat solution and works great. (You can even ignore the warning controller passed to this method and just check that the view manager passed the presentViewController method ...)

+5
source

This can also be done as follows:

Let's say we have a button that, when clicked, shows the view controller:

 - (void) didTapButton { UIAlertController* c = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:c animated:ANIMATED completion:nil]; } 

Note that the ANIMATED parameter is not "YES" or "NO". In PrefixHeader it is defined as:

 #define ANIMATED (getenv("runningTests") == NULL) 

and runTests is the environment variable defined in the test task. We do not want animation when running unit / integration tests.

The testing method looks like this:

 - (void) testButtonWillShowAlertView { UIApplication.sharedApplication.delegate.window.rootViewController = controller; [controller.button sendActionsForControlEvents:UIControlEventTouchUpInside]; XCTAssertEqualObjects(controller.presentedViewController.class, UIAlertController.class); } 

Important line

 UIApplication.sharedApplication.delegate.window.rootViewController = controller; 

Obviously, the rootViewController on the UIWindow must be installed.

+1
source

I wrote a wrapper around the UIAlertController for easier unit testing.

You can check if this is visible

 XCTAssert(testableAlert.visible) 

And you can also perform its actions

 testableAlert.simulateAction("OK") 

https://github.com/exchangegroup/TestableAlert

0
source

You can simply verify the existence of a UIAlertController using the code below (target c).

 XCTAssertFalse(app.alerts.element.staticTexts[@"your alert message"].exists); 

the test will fail if the alert is not displayed, otherwise you can use

 app.alerts.element.staticTexts[@"your alert message"].exists 

using if or XCTAssertTrue.

0
source

Source: https://habr.com/ru/post/976324/


All Articles