0

我有一个 Mac 应用程序,实际上是一个 NSStatusItem,我正在尝试测试它的首选项窗口。我将此代码用作模型,因为它的工作方式与我的代码非常相似,只是我的窗口已初始化并显示在 applicationDidFinishLaunching 以外的函数中。 http://bit.ly/wH6QOk

我收到错误:未调用预期的方法:窗口

我不明白为什么会这样。这是我的测试代码:

-(void)testPreferencesInit {    
    mockWindowController = [OCMockObject mockForClass:[NSWindowController class]];
    id mockWindow = [OCMockObject mockForClass:[NSWindow class]];

    [[[mockWindowController expect] andReturn:mockWindow] window];
    [[mockWindow expect] makeKeyAndOrderFront:weathervane];

    [weathervane showPreferences:nil];

    [mockWindowController verify];
    [mockWindow verify];

    id windowController;
    object_getInstanceVariable(weathervane, "windowController", (void **)&windowController);
    GHAssertEqualObjects(windowController, mockWindowController,
                                @"windowController not set on appDelegate");
    GHAssertTrue([[windowController windowNibName] isEqualToString:@"Preferences"], @"Window Controller nib name not set");

    object_setInstanceVariable(weathervane, "windowController", nil);
    mockWindowController = nil;
}
4

1 回答 1

0

从您的代码中不清楚您windowController期望mockWindowController. 您是否invokeSupersequent按照 Matt Gallagher 的帖子实现了类别初始化程序和代码?

从您的代码中可以看出,它windowController是风向标类的属性。我认为最简单的方法是避免尝试覆盖NSWindowController的初始化,而只是将您的模拟传递给您的控制器:

-(void)testPreferencesInit {    
    mockWindowController = [OCMockObject mockForClass:[NSWindowController class]];
    id mockWindow = [OCMockObject mockForClass:[NSWindow class]];

    [[[mockWindowController expect] andReturn:mockWindow] window];
    [[mockWindow expect] makeKeyAndOrderFront:weathervane];

    weathervane.windowController = mockWindowController;

    [weathervane showPreferences:nil];

    [mockWindowController verify];
    [mockWindow verify];
}
于 2012-02-19T05:26:21.600 回答