首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试NSDocument

测试NSDocument
EN

Stack Overflow用户
提问于 2013-07-11 10:56:23
回答 1查看 227关注 0票数 1

我刚刚开始单元测试,我想知道如何正确地测试NSDocument子类?

在我的测试setUp中,我可以初始化文档,但这不能反映应用程序使用它时文档是如何设置的,特别是没有建立IBOutlet连接,也没有调用关键消息,比如- (void)windowControllerDidLoadNib:(NSWindowController *)aController

那么,获得用于测试的完全初始化的NSDocument对象的正确方法是什么呢?

EN

回答 1

Stack Overflow用户

发布于 2015-09-02 02:30:03

这就是你如何开始的:

代码语言:javascript
复制
#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

代码语言:javascript
复制
- (void)makeWindowControllers
{
  CustomWindowController *controller = [[CustomWindowController alloc] init];
  [self addWindowController:controller];
}

从窗口控制器,您可以随时访问您的文档

代码语言:javascript
复制
- (CustomDocument *)document
{
  return [self document];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17584288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档