gpt4 book ai didi

ios - 如何使用 XCTest 测试导航栏是否未设置为 objective-C 中的特定颜色?

转载 作者:行者123 更新时间:2023-11-29 03:06:19 27 4
gpt4 key购买 nike

尝试测试 TableView Controller 的导航栏颜色。我已经在 TableView Controller 中设置了 navigationController 的 barTintColor。

我写了下面的测试:

- (void)testNavBarColourOfMasterViewController
{
VAGMasterViewController *mvc = [[VAGMasterViewController alloc] init];
[mvc view];
XCTAssertEqualObjects([[[mvc navigationController] navigationBar] barTintColor], [UIColor whiteColor]);
}

错误:

test failure: -[VAGMasterViewControllerTests testNavBarColourOfMasterViewController] failed: (([[[mvc navigationController] navigationBar] barTintColor]) equal to ([UIColor whiteColor])) failed: ("(null)") is not equal to ("UIDeviceWhiteColorSpace 1 1")

显然,当我尝试读取 barTintColor 的颜色时,它为空。在controller中的viewDidLoad中设置怎么会这样呢?

亲切的问候。

最佳答案

仅创建 View Controller 将导致导航 Controller (以及导航栏)nil。解决方案是创建一个导航 Controller :

- (void)testNavBarColourOfMasterViewController
{
VAGMasterViewController *mvc = [[VAGMasterViewController alloc] init];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:mvc];
[mvc view];
XCTAssertEqualObjects([[[mvc navigationController] navigationBar] barTintColor], [UIColor whiteColor]);
}

此外,在 viewDidLoad 中访问导航 Controller 或其属性(在本例中为 bar)也不是一个好习惯,因为 View 加载可能会在导航 Controller 将自身链接到之前触发。 View Controller ,导致问题。

关于ios - 如何使用 XCTest 测试导航栏是否未设置为 objective-C 中的特定颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22738294/

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