ios - How to write unit test for testing view controller? -


we trying write our custom unit tests testing every view controller separately. know there ui automation, kif, , other solutions, if take @ code above find should possible without using them.

so, simplicity, suppose there's simple view controller 2 public methods "setfirststate" , "setsecondstate". need test, "setfirststate" method called when view controller instantiated , shown on screen (goes through viewdidload). here's demo view controller:

@interface myviewcontroller     - (void)setfirststate;   - (void)setsecondstate;  @end  @implementation myviewcontroller    - (void)viewdidload {      [super viewdidload];      [self setfirststate];   }  @end 

so, unit test testing setfirststate call (using xctest , ocmock):

- (void)test_viewdidload_callsmymethod {    myviewcontroller *viewcontoller = [[myviewcontroller alloc] init];    id mock = [ocmockobject partialmockforobject:viewcontoller];   [[mock expect] showlogin];    [uiapplication sharedapplication].keywindow.rootviewcontroller = (myviewcontroller *)mock;    [mock verify]; } 

the interesting code line 1 assigning mock (on viewcontroller) keywindow's rootviewcontroller. found need if want methods viewdidload, viewwillappear called. otherwise won't called. if run tests simulator hangs on line. xcode shows it's running , still alive doesn't go further.

we tried replacing keywindow's rootviewcontroller code line "[mock viewdidload]" simulating , testing viewdidload, hangs. so, looks partial mock breaks internal flow of appearing view controller.

if remove mock , assign view controller directly keywindow's rootviewcontroller works , can test other things. however, maybe have idea how fix that?

update forgot mention use xibs instead of storyboards, every component in our system loosely coupled.

it turns out newrelic library breaking ocmock :). therefore works after commenting out:

//[newrelicagent startwithapplicationtoken:@"token"]; 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -