這是你如何開始:
#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)如果你想對它進行測試。單一責任。怎麼樣?只實現makeWindowControllers
- (void)makeWindowControllers
{
CustomWindowController *controller = [[CustomWindowController alloc] init];
[self addWindowController:controller];
}
從窗口控制器,你可以訪問你的文件隨時隨地
- (CustomDocument *)document
{
return [self document];
}