Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional testing in Swift. Simulate the Application flow

I'm trying to perform some really simple feature/functional testing in Swift but I have some doubts that I need to resolve to be able to create useful tests.

I want to verify that a Controller presented by another Controller exists into the Application Navigation Hierarchy (it doesn't matter if the Controller has been presented into a NavigationController, as Modal or whatever).

If I instantiate and show controllers programmatically, directly into the test functions, when I check the On Top controller I always get the Storyboard root controller instead of the controller that I have just instantiated, as if the controllers that I've manually created are never added into the Application Hierarchy.

Here an example of pseudo-code:

func testController(){ 

    // Instantiate a controller 
    let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
    let controller1 = storyBoard.instantiateViewControllerWithIdentifier("Controller1") as? ControllerOneViewController
    controller1.loadView()

    // Call a function that instantiates another controller 
    controller1.pushAnotherController()

    // Test that the current shown controller is what we expect... 
    let rootController = UIApplication.sharedApplication().keyWindow?.rootViewController
    XCTAssert(rootController.self == TheExpectedClass, "Controller is not what we expect")
}
like image 248
MatterGoal Avatar asked Dec 07 '14 20:12

MatterGoal


People also ask

What is functional testing with real time example?

White box testing example: In this functional testing example, consider an end-to-end test for a customer who adds payment information to a retailer's app. Developers and testers would conduct tests in a white-box format to ensure that sensitive data, such as a credit card number, is stored in a PCI-compliant manner.

What is the purpose of testing explain functional testing in detail?

Functional testing is a type of testing that seeks to establish whether each application feature works as per the software requirements. Each function is compared to the corresponding requirement to ascertain whether its output is consistent with the end user's expectations.


1 Answers

If I instantiate and show controllers programmatically, directly into the test functions, when I check the On Top controller I always get the Storyboard root controller instead of the controller that I have just instantiated, as if the controllers that I've manually created are never added into the Application Hierarchy.

From the code you wrote you are not checking the On Top controller but you are checking root view controller itself (which holds all view controllers in hierarchy including navigation controllers) so thats why you get always storyboard root view controller back. To get top most controller from view controller you can use the following recursive function which takes root view controller and return its top most controller

func topMostController(rootViewController:UIViewController)->UIViewController{

    if let viewController = rootViewController as? UINavigationController{

        return topMostController(viewController.visibleViewController)
    }

    if let viewController = rootViewController.presentedViewController{

        return topMostController(viewController)
    }

    return rootViewController
}

and then in your testing function check the controller that this function returns

func testController(){ 

// Instantiate a controller 
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
let controller1 = storyBoard.instantiateViewControllerWithIdentifier("Controller1") as? ControllerOneViewController
controller1.loadView()

// Call a function that instantiates another controller 
controller1.pushAnotherController()

// Test that the current shown controller is what we expect... 
let rootController = UIApplication.sharedApplication().keyWindow?.rootViewController
XCTAssert(topMostController(rootController) == TheExpectedClass, "Controller is not what we expect")
}
like image 148
Zell B. Avatar answered Sep 25 '22 23:09

Zell B.