How do I use XCTest to test if a navigationBar is not set to a specific colour in objective-C?

大憨熊 提交于 2019-12-11 14:57:07

问题


Trying to test for navigationBar colour of a table view controller. I've set the barTintColor of the navigationController in the table view controller.

I have written the following test:

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

Error:

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

Apparently when I try to read the colour of the barTintColor it is null. How can this be if it's set in viewDidLoad in the controller?

Kind regards.


回答1:


Creating just a view controller will lead to the navigation controller (and thus, the bar) being nil. The solution is to create a navigation controller:

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

Also, it is not good practice to access the navigation controller or its properties (in this case, bar) in viewDidLoad, because the view loading may be triggered before the navigation controller has linked itself with the view controller, causing issues.



来源:https://stackoverflow.com/questions/22738294/how-do-i-use-xctest-to-test-if-a-navigationbar-is-not-set-to-a-specific-colour-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!