gpt4 book ai didi

objective-c - 在 UIViewController 上设置只读 navigationController 属性以进行模拟

转载 作者:太空狗 更新时间:2023-10-30 03:28:02 24 4
gpt4 key购买 nike

我使用 OCMock 创建了一个模拟的 UINavigationController。但是,我无法将它分配给 UIViewController 的 navigationController 属性,因为该属性是只读的。

id mockNavController = [OCMockObject mockForClass:[UINavigationController class]];
...
myViewController.navigationController = mockNavController; // readonly!

作者 this blog post声称已找到解决方案但忽略了分享。

最佳答案

不必创建允许您设置 navigationController 属性的更改器,因为您可以模拟返回它的访问器。以下是我的做法:

-(void)testTappingSettingsButtonShouldDisplaySettings {
MyController *myController = [[MyController alloc] init];

// expect the nav controller to push a settings controller
id mockNavigationController = [OCMockObject mockForClass:[UINavigationController class]];
[[mockNavigationController expect] pushViewController:[OCMArg any] animated:YES];

// set up myController to return the mocked navigation controller
id mockController = [OCMockObject partialMockForObject:myController];
[[[mockController expect] andReturn:mockNavigationController] navigationController];

[myController settingsButtonTapped];

[mockNavigationController verify];
[mockController verify];
[myController release];
}

关于objective-c - 在 UIViewController 上设置只读 navigationController 属性以进行模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4514613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com