[英]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.