mercredi 6 mai 2015

Testing navigation in XCode Unit tests

I am trying to test UIViewControllers navigation in Unit tests, I was able to do the backend testing but to increase the coverage of tests wanted to test UIViewControllers,

Facing a strange issue so I have a ViewController (created manually without Storyboard) which has two buttons and clicking each of these buttons takes you another view (Pushes the new view on Navigation Controller).

The initialisation is as follows in viewDidLoad

// Setup the action of button
[self.button1 addTarget:self
                 action:@selector(goToView1)
       forControlEvents:UIControlEventTouchUpInside];

 [self.button2 addTarget:self
                  action:@selector(goToView2)
        forControlEvents:UIControlEventTouchUpInside];

here is my code for pushing these two views

- (void)goToView1 
{
    // Go to the FAQ Screen
    ViewController1 *viewController = [[ViewController1 alloc] init];
    [self.navigationController pushViewController:viewController     
                                         animated:YES];
}
- (void)goToView2
{
      // Go to the checkin screen
      ViewController2 *viewController =[[ViewController2 alloc] init];
      [self.navigationController pushViewController:viewController
                                           animated:YES];
}

In my testclass I am trying to test it like this

- (void)testNavigation 
{
    //Initialize the view controller and call the navigation functions
    [self.testController goToView1];

    XCTAssertTrue([self.navigationController.topViewController 
                        isKindOfClass:[ViewController1 class]], 
                        @"Did not navigate Controller 1");

    [self.navigationController popToViewController:self.testController 
                                          animated:NO];

    [self.testController goToView2];

    XCTAssertTrue([self.navigationController.topViewController 
                  isKindOfClass:[ViewController2 class]], 
                  @"Did not navigate to Controller 2");

}

So this test actually fails the reason I guess is because I am checking for the ViewController immediately after i call the PushViewcontroller with animated:YES, If I push the view controllers without Animation test passes (because the view is immediately pushed and the Assert checks out ok).

So My question is, is there anyway I can put a hook some where to check if the View has been pushed before checking the Assert (I dont want to turn of the animation just for passing the test as it will kill the whole purpose) also I dont want to put any aribitary delay. Can I somehow check if the push animation has completed and view is finally on top before running the test logic?

Aucun commentaire:

Enregistrer un commentaire