[英]Unit testing IBOutlet's properties
我使用GHUnit
。 我想測試IBOutlet
的屬性,如isHidden
, delegate
等。
我嘗試下面的代碼來測試myView
是否被隱藏:
- (void)testViewDidLoad
{
// Call view on viewcontroller which will load the view if not loaded
[testClass view];
// Tests
testClass.myView.hidden = YES;
if (testClass.myView.isHidden) {
GHTestLog(@"Hidden");
}
else {
GHTestLog(@"Not Hidden");
}
}
這總是記錄Not Hidden
,而在記錄之前我將其設置為hidden = YES
。
為什么是這樣?
為了測試IBOutlet
delegate
屬性,我嘗試了以下行:
GHAssertNotNil(testClass.textField.delegate, @"delegate is nil.");
它因Reason : ((testClass.textField.delegate) != nil) should be FALSE.
失敗Reason : ((testClass.textField.delegate) != nil) should be FALSE.
怎么了?
編輯:嘗試下面的代碼不起作用。
[testClass view];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:[NSBundle bundleForClass:[self class]]];
GHAssertNotNil(storyboard, nil);
// Tests
GHAssertTrue(testClass.myView.isHidden, nil); // This fails
在測試UIStoryboard
包含的UIStoryboard
,請確保已將故事板文件添加到測試目標。
我還建議您進行一項測試,驗證您的故事板是否已成功初始化,如下所示:
- (void)testStoryboardShouldBeInitialized
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NameOfStoryboard" bundle:[NSBundle bundleForClass:[self class]]];
STAssertNotNil(storyboard, nil);
}
編輯
您首先必須實例化您想要從故事板中測試的視圖控制器,然后您可以測試您的插座:
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerIdentifier"];
[controller view];
GHAssertTrue(controller.myView.isHidden, nil);
我還建議(以單獨的方法)測試您的視圖控制器是否已成功實例化。 我還將控制器初始化移動到setUp方法中 - 如有必要。
這是因為接口構建器上的IBTextField無法找到已定義的文本字段屬性。
那就是你需要附加它或者從界面構建器將textfield連接到它的屬性。
(或者可能是因為您的testClass
無法指向或提供對textField
訪問權限。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.