2013-07-11 4 views
1

나는 단위 테스트를 시작하고 NSDocument 서브 클래스를 올바르게 테스트하는 방법을 알고 싶습니다.NSDocument 테스트하기

내 테스트 setUp에서 문서를 초기화 할 수는 있지만 문서가 응용 프로그램에서 사용될 때 설정 방법을 반영하지 않습니다. 특히 IBOutlet 연결이 만들어지지 않고 - (void)windowControllerDidLoadNib:(NSWindowController *)aController과 같은 중요한 메시지가 호출되지 않습니다. .

그렇다면 테스팅에 사용하기 위해 완전히 초기화 된 NSDocument 객체를 얻는 올바른 방법은 무엇입니까?

답변

0

이 시작할 수있는 방법입니다 :

#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]; 

올바른 접근 방식 : 당신이 그것을 테스트하려는 경우 이 문서 (windowControllerDidLoadNib)에 어떤 UI 물건을 넣지 마십시오. 단일 책임. 방법? 그냥 당신이 언제

- (CustomDocument *)document 
{ 
    return [self document]; 
} 
문서에 액세스 할 수 있습니다 창 컨트롤러에서 makeWindowControllers

- (void)makeWindowControllers 
{ 
    CustomWindowController *controller = [[CustomWindowController alloc] init]; 
    [self addWindowController:controller]; 
} 

를 구현