2013-07-15 12 views
1

我在測試ViewController內的某些代碼(某些控件是活動的,取決於某些UISwitches等的狀態),並決定去獼猴桃的它,因爲我們正在使用它進行其他一些低級邏輯測試。是否有可能實例化視圖控制器從獼猴桃測試故事板

我的期望是這樣運行測試:

__block AViewController *aVC; 

it(@"(tokenTextField) should be hidden if the token switch is set to off", ^{ 
    lvC.useTokenSwitch.on = false; 
    [[theValue(aVC.tokenTextField.hidden) should] equal:theValue(YES)]; 
}); 

我的問題是與AViewController的初始化。如果我這樣做:

aVC = [[AViewController alloc] initWithNibName:@"aViewController" bundle:nil]; 

我會得到一個「AViewController」沒有任何初始化控制,所以我必須手動初始化它們。

所以我試圖獲得AViewController這樣做:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
aVC = [storyboard instantiateViewControllerWithIdentifier:@"AViewController"]; 

然而,這導致一個錯誤消息:

NSInvalidArgumentException「找不到捆綁一個NSBundle <名爲 'MainStoryboard' 故事板/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/Developer/usr/bin >(加載)」提出

我已將MainStoryboard包含在我的測試目標中,並將其包含在「Build Phases」 - >「Copy Bundle Resources」中,但仍然沒有任何內容。

所以我想知道是否甚至有可能在Kiwi測試目標中從Storyboard實例化ViewController? (因爲我沒有在任何地方看到它的任何例子)。

我的方法錯了,我應該嘲笑ViewController?

我錯過了什麼可以包含在測試目標中嗎?

+0

我可以用這種方式實例化故事板。實際上,我*不會*在測試目標或複製包資源構建階段包含故事板,但我只是嘗試了兩種方式,任何一種方式都適用於我。 –

+0

謝謝,在玩弄之後,我設法成功實例化了它。但現在我卡住了,然後我執行: [aVC performSelectorOnMainThread:@selector(loadView)withObject:nil waitUntilDone:YES]; 因爲它導致:NSInvalidArgumentException「 - [__ NSCFType pointSize]:發送到實例0x21f4690的無法識別的選擇器」 – Katagelon

回答

0

問題是你正在爲你的包傳遞nil。您可以在錯誤消息中看到它正在使用的包。在單元測試中,你可能想要做這樣的事情:

Class viewControllerClass = [myViewController class]; 

NSString *className = NSStringFromClass(viewControllerClass); 
NSBundle *classBundle = [NSBundle bundleForClass:viewControllerClass]; 

MyViewController *viewController = 
[[MyViewController alloc] initWithNibName:className 
            bundle:classBundle]; 

通過查找包含您的視圖控制器類的捆綁,你還可以得到它的筆尖文件。

對於情節串連圖板,則代碼:

Class viewControllerClass = [myViewController class]; 

NSString *className = NSStringFromClass(viewControllerClass); 
NSBundle *classBundle = [NSBundle bundleForClass:viewControllerClass]; 

UIStoryboard *storyboard = 
[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:classBundle]; 

MyViewController *viewController = 
[storyboard instantiateViewControllerWithIdentifier:className]; 

(這一切都假定你的筆尖的名字和故事板標識符匹配的類名稱。如果他們不這樣做,改變它。)

0

「AViewController」沒有任何控制初始化

是因爲所有這些控件的初始化是屬於查看,你需要調用[viewCo ntroller loadView]來初始化UI組件。

如果您還使用viewDidLoad func來初始化一些UI組件,則必須調用[viewController view]來觸發viewDidLoad函數。

相關問題