簡體   English   中英

未調用單元測試情節提要板viewDidLoad

[英]Unit Testing Storyboard viewDidLoad not called

因此,有幾篇關於如何加載包含筆尖的Storyboard進行單元測試的文章,看起來就像這樣

MyViewController vc;

- (void)setUp
{
    [super setUp];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.vc = [storyboard instantiateViewControllerWithIdentifier:@"main"];
    [self.vc performSelectorOnMainThread:@selector(loadView) withObject:nil waitUntilDone:YES];
}

但是,當我這樣做時,永遠不會調用該類的viewDidLoad方法,因此我必須像這樣手動進行

- (void)setUp
{
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MediaSelectionSuite" bundle:[NSBundle mainBundle]];
    SUT = [storyboard instantiateViewControllerWithIdentifier:@"Gallery"];
    [SUT performSelectorOnMainThread:NSSelectorFromString(@"loadView") withObject:nil waitUntilDone:YES];
    [SUT viewDidLoad];
}

如您所見,我必須添加[SUT viewDidLoad]; 完全可以調用該控制器的viewDidLoad ...它可以工作,我不應該太擔心,因為它是一組測試,但是我對此感到擔憂,因為您很少需要手動調用它,也不應該instantiateViewControllerWithIdentifier:或調用loadView的選擇器確保viewDidLoad被調用?

UPDATE

一個實驗位后,它似乎只有UIViewController已經設置為你的故事板最初UIViewController有它viewDidLoad呼吁方法instantiateViewControllerWithIdentifier:

我最近遇到了與您相同的情況,並找到了解決方案。 您是正確的,不會自動調用viewDidLoad()。 實際上,您可以調用視圖控制器的view屬性來觸發viewDidLoad()。 有關更多信息,請查看View Controller TDD

暫無
暫無

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

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