我有一个简单的 Mac OS 应用程序,默认情况下MainMenu.xib
。在那里我有第二个窗口用于偏好设置和PreferencesWindowController
。我想让以下测试工作:
@implementation TestPreferencesWindow
- (void)testProtectsUserPasswordByUsingAPasswordField
{
PreferencesWindowController *controller = [[PreferencesWindowController alloc] initWithWindowNibName:@"MainMenu"];
XCTAssertInstanceOf([[controller passwordField] class], NSSecureTextField);
}
@end
问题是[controller passwordField]
没有初始化(因为笔尖没有加载?)所以它总是返回nil
.
如何告诉笔尖创建所有绑定?
当我打电话时[controller window]
给出错误,并返回nil
:
Could not connect the action orderFrontStandardAboutPanel: to target of class PreferencesWindowController
为了调试我尝试了以下方法:
NSBundle *bundle = [NSBundle mainBundle];
XCTAssertNotNil(bundle);
NSString *nibPath = [bundle pathForResource:@"MainMenu" ofType:@"nib"];
XCTAssertNotNil(nibPath);
PreferencesWindowController *controller = [[PreferencesWindowController alloc] initWithWindowNibPath:nibPath owner:self];
NSLog(@"%@ %@", [controller window], [controller passwordField]);
但它仍然打印(null) (null)
...
要消除警告:
NSBundle *bundle = [NSBundle mainBundle];
XCTAssertNotNil(bundle);
NSString *nibPath = [bundle pathForResource:@"MainMenu" ofType:@"nib"];
XCTAssertNotNil(nibPath);
NSApplication *app = [NSApplication sharedApplication];
PreferencesWindowController *controller = [[PreferencesWindowController alloc] initWithWindowNibPath:nibPath owner:app];
NSLog(@"%@ %@", [controller window], [controller passwordField]);
不再有警告,但仍然打印(null) (null)
..我觉得我越来越接近了...