问题描述
我有一个简单的Mac OS应用程序,它带有默认的MainMenu.xib
.在这里,我有第二个偏好设置窗口和PreferencesWindowController
.我想进行以下测试:
I have a simple Mac OS application that comes with the default MainMenu.xib
. In there I have a second window for preferences and a PreferencesWindowController
. I'd like to get the following test working:
@implementation TestPreferencesWindow
- (void)testProtectsUserPasswordByUsingAPasswordField
{
PreferencesWindowController *controller = [[PreferencesWindowController alloc] initWithWindowNibName:@"MainMenu"];
XCTAssertInstanceOf([[controller passwordField] class], NSSecureTextField);
}
@end
问题在于[controller passwordField]
尚未初始化(因为笔尖未加载?),因此它始终返回nil
.
The problem is that [controller passwordField]
is not initialised (because the nib isn't loading?) so it always returns nil
.
我如何告诉笔尖创建所有绑定?
How do I tell the nib to create all the bindings?
当我呼叫[controller window]
时给出错误,并返回nil
:
When I call [controller window]
is gives the error, and returns nil
:
Could not connect the action orderFrontStandardAboutPanel: to target of class PreferencesWindowController
对于调试,我尝试了以下方法:
For debugging I have tried the following:
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)
...
However it still prints (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)
..我觉得我越来越近了...
No more warning, but still prints (null) (null)
.. I feel like I'm getting closer though...
推荐答案
我得到了解决方案.在您的.m文件中编写以下方法,并在.h文件中声明public
I got solution. Write below method in your .m file, and declare public in .h file
MyWindowController.m文件
- (instancetype)initWithWindowNibPath:(NSString *)nibPath {
self = [super initWithWindowNibPath:nibPath owner:self];
if(self)
{
//initialize stuff
}
return self;
}
MyWindowController.h文件
- (instancetype)initWithWindowNibPath:(NSString *)nibPath;
现在编写您的代码:
NSBundle *bundle = [NSBundle mainBundle];
XCTAssertNotNil(bundle);
NSString *nibPath = [bundle pathForResource:@"MyWindowController" ofType:@"nib"];
XCTAssertNotNil(nibPath);
MyWindowController *controller = [[MyWindowController alloc] initWithWindowNibPath:nibPath];
NSLog(@"%@ %@", [controller window], [controller passwordField]);
这对我来说很完美.
原因:所有者值未在initWithWindowNibPath方法中正确设置,因此未将类的属性NSOutlet设置为nib的控件.
Reason:owner value not properly set in initWithWindowNibPath method and therefore not set class's properties NSOutlet to nib's control.
这篇关于是否可以对用笔尖初始化的WindowController进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!