簡體   English   中英

如何使用XCTest測試navigationBar是否在Objective-C中未設置為特定顏色?

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

嘗試測試表視圖控制器的navigationBar顏色。 我已經在表視圖控制器中設置了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的顏色時,它為null。 如果在控制器的viewDidLoad中設置了該怎么辦?

親切的問候。

僅創建視圖控制器將導致導航控制器(因此,條形)為nil 解決方案是創建一個導航控制器:

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

同樣,在viewDidLoad訪問導航控制器或其屬性(在本例中為bar)也不是一個好習慣,因為在導航控制器將自身鏈接到視圖控制器之前,可能會觸發視圖加載,從而導致問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM