Best way to test storyboards with OCMockito

173 Views Asked by At

I'm trying to test an application based on storyboards. The scenario is a Navigation Controller with a View Controller inside. The test is that performing a segue sets a view controller as destination controller (push segue). I'm using OCMockito and here's my test:

MyViewController *mockSut = mock([MyViewController class]);
UINavigationController *nav = mock([UINavigationController class]);

[given(mockSut.navigationController) willReturn:nav];
[mockSut performSegueWithIdentifier:@"mySegue" sender:nil];

MKTArgumentCaptor *segueArgument = [[MKTArgumentCaptor alloc] init];
MKTArgumentCaptor *senderArgument = [[MKTArgumentCaptor alloc] init];

[verify(mockSut) prepareForSegue:[segueArgument capture] sender:[senderArgument capture]];
assertThat([[[segueArgument value] destinationViewController] class], equalTo([MyDestinationViewController class]));

But it complains that prepareForSegue: method was not invoked.

What's the problem here? Maybe that performSegueWithIdentifier: invokes prepareForSegue: in an asynchronous way?

1

There are 1 best solutions below

0
On

Who calls -prepareForSegue:sender:? UIKit does. By trying to test that, you're testing UIKit — which is unnecessary. When dealing with a framework (that is, something that calls back to you), the basic pattern is:

  • Ensure that you're calling the framework correctly.
  • Assume the framework calls back correctly. Pretend that this happened by having your test directly call the method which the framework would call.