objective c - Testing NSDocument -
i'm getting started unit testing , i'm wondering how correctly test nsdocument subclass?
in test setup can init document doesn't reflect how document setup when it's used app, iboutlet connections aren't made , critical messages such - (void)windowcontrollerdidloadnib:(nswindowcontroller *)acontroller
aren't called.
so what's correct way initialized nsdocument object use in testing?
this how can start:
#import <cocoa/cocoa.h> #import <xctest/xctest.h> #import "document.h" @interface documenttests : xctestcase { document *document; nswindowcontroller *controller } @end @implementation documenttests - (void)setup { document = [[document alloc] init]; [document makewindowcontrollers]; controller = (nswindowcontroller *)[document windowcontrollers][0]; } - (void)testloadingwindow { xctassertnotnil(controller.window); } - (void)testtextfieldoutletsisconnected { [controller window]; //kick off window loading xctassertnotnil(document.textfield); } //for asynchronous testing use xctestexpectation //[self expectationwithdescription:@"expectations"]; //[self waitforexpectationswithtimeout:3.0 handler:nil];
correct approach: not put ui stuff document (windowcontrollerdidloadnib) if want test it. single responsibility. how? implement makewindowcontrollers
- (void)makewindowcontrollers { customwindowcontroller *controller = [[customwindowcontroller alloc] init]; [self addwindowcontroller:controller]; }
from window controller can access document anytime
- (customdocument *)document { return [self document]; }
Comments
Post a Comment