我刚刚开始单元测试,我想知道如何正确地测试NSDocument子类?
在我的测试setUp中,我可以初始化文档,但这不能反映应用程序使用它时文档是如何设置的,特别是没有建立IBOutlet连接,也没有调用关键消息,比如- (void)windowControllerDidLoadNib:(NSWindowController *)aController。
那么,获得用于测试的完全初始化的NSDocument对象的正确方法是什么呢?
发布于 2015-09-02 02:30:03
这就是你如何开始的:
#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内容放入其中。单一的责任。多么?只需实现makeWindowControllers
- (void)makeWindowControllers
{
CustomWindowController *controller = [[CustomWindowController alloc] init];
[self addWindowController:controller];
}从窗口控制器,您可以随时访问您的文档
- (CustomDocument *)document
{
return [self document];
}https://stackoverflow.com/questions/17584288
复制相似问题