0
votes

I am trying to unit test an unwind segue.

But it seems that in the test calling performSegueWithIdentifier: method doesn't perform an unwind segue while it does for other kinds of segues, e.g. Push and Modal segues.

Basically what I am trying to do is the following:

  1. Get the view controller from storyboard.
  2. Perform the unwind segue programmatically with this line of code:

    [vc performSegueWithIdentifier:@"unwind" sender:vc];

    (I have tried to change the sender to nil and vc.button which is the triggering button for the unwind segue. But the test still failed.)

  3. Check if the seguePerformed property in the view controller is set to YES with this line of code:

    XCTAssertTrue(sut.seguePerfomed, @"should be true.");

    In this view controller's - prepareForSegue: method, I am using the identifier of a segue to identify the unwind segue. If it is the unwind segue then set the view controller's seguePerformed property to YES.

I thought there was something wrong with my performSegue code. However, if I copy this line [vc performSegueWithIdentifier:@"unwind" sender:vc]; into my view controller's viewDidAppear method to test it, and enter this view in an iPhone simulator. It does return to its previous view just after entering it.

Sorry that I forgot to mention that I also tried the following snippet:

- (void)testPerformingSegueShouldSetSeguePerform
{
    // given
    UINavigationController *nvVC = [sut.storyboard instantiateViewControllerWithIdentifier:@"nvVC"];

    [nvVC pushViewController:sut animated:NO];

    sut = (NTDSubViewController *)nvVC.topViewController;

    // when
    [sut performSegueWithIdentifier:@"unwind" sender:sut.button];

    // then
    XCTAssertTrue(sut.seguePerfomed, @"should be true.");
}
1
I tried to assert on changes of the NavigationController's state. Strangely, neither the viewControllers count changed, nor did the topViewController change. Even in the simulator, it takes more than a single unwind call to see these changes in the destination VC right away. Don't know what's missing though.Any luck on your side? - ctietze

1 Answers

0
votes

An unwind segue only makes sense in live context, because it has to do with instances, not classes. For example, if I start in View Controller A and I push View Controller B, then I can unwind, popping to View Controller A - not just any View Controller A, but the very instance that we started with.

But you are just pulling View Controller B out of the storyboard and trying to unwind. You can't because there is no View Controller A to unwind to. For this to work, you must do exactly what I just described: start with View Controller A, push to View Controller B, and now tell that View Controller B to unwind.