View Controller Test: Alert
View Controller Test: Alert avatar

Testing alerts in Swift is a bit tricky because it is not possible to directly access the handler of an UIAlertAction. First, you need to introduce a member variable in the view controller for the UIAlertAction, which can be overwritten by a mock. Next, you need to extend UIAlertAction by a function, which receives the handler and creates the action. This function can then be overwritten in UIAlertActionMock to store the handler and call it inside the test. Tough shit. In the following, a full example is shown:

View Controller:

Test Class:

View Controller Test: Segue
View Controller Test: Segue avatar

To test if an ACTION of a view controller CONTROLLER triggers a certain SEGUE use the following function: