iphone - Testing NSWidowController using OCMock -
i've been trying come a way unit test applicationdidfinishlaunching delegate using ocmock. nswindowcontroller instantiated here , i'd test it. here's test code:
id mockwindowcontroller = [ocmockobject nicemockforclass:[urltimerwindowcontroller class]]; [[mockwindowcontroller expect] showwindow:self]; nsuinteger preretaincount = [mockwindowcontroller retaincount]; [appdelegate applicationdidfinishlaunching:nil]; [mockwindowcontroller verify]; when run test, error:
"ocmockobject[urltimerwindowcontroller]: expected method not invoked: showwindow:-[urltimerappdelegatetests testapplicationdidfinishlaunching]"
the log gives more detail:
"test case '-[urltimerappdelegatetests testapplicationdidfinishlaunching]' started. 2011-04-11 08:36:57.558 otest-x86_64[3868:903] -[urltimerwindowcontroller loadwindow]: failed load window nib file 'timerwindow'. unknown.m:0: error: -[urltimerappdelegatetests testapplicationdidfinishlaunching] : ocmockobject[urltimerwindowcontroller]: expected method not invoked: showwindow:-[urltimerappdelegatetests testapplicationdidfinishlaunching] test case '-[urltimerappdelegatetests testapplicationdidfinishlaunching]' failed (0.005 seconds). " so see nib fails load. ok, how make load while unit testing or somehow mock load? i've looked @ ocmock docs, chris hanson's tips on unit testing , few other resources, including whereismymac source code behave in similar fashion. application instantiating window controller this:
self.urltimerwindowcontroller = [[urltimerwindowcontroller alloc] init]; [self.urltimerwindowcontroller showwindow:self]; any tips appreciated.
the problem test mockwindowcontroller , urltimerwindowcontroller not same object. , self in test not same self in class under test. doesn't matter nib doesn't load in case.
you can't mock object when it's instantiated inside method want test. 1 alternative instantiate object in 1 method, pass method finishes setup. can test setup method. example:
-(void)applicationdidfinishlaunching:(nsnotification *)anotification { self.urltimerwindowcontroller = [[urltimerwindowcontroller alloc] init]; [self setuptimerwindow:urltimerwindowcontroller]; } -(void)setuptimerwindow:(urltimerwindowcontroller *)controller { [controller showwindow:self]; } then, test setuptimerwindow::
-(void)testsetuptimerwindowshouldshowwindow { urltimerappdelegate *appdelegate = [[urltimerappdelegate alloc] init]; id mockwindowcontroller = [ocmockobject nicemockforclass:[urltimerwindowcontroller class]]; [[mockwindowcontroller expect] showwindow:appdelegate]; // seems weird. showwindow take app delegate parameter? [appdelegate setuptimerwindow:mockwindowcontroller]; [mockwindowcontroller verify]; [appdelegate release]; }
Comments
Post a Comment