2014-01-15 13 views
2

我正在使用XCTestOCMock的組合來測試iOS應用程序。我正在使用UITableViewController來呈現一系列我希望爲其編寫測試的原型單元格。單元格本身在故事板中,所以我不相信我可以從筆尖實例化它。有沒有什麼辦法從故事板實例化原型UITableViewCell進行測試?

是使用使​​用電池初始化它viewController唯一的選擇?

我使用的自定義單元格類有很多的「IBOutlets」連接到原型的故事板。 Cell類看起來是這樣的:

@interface QRFeedAdCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UIImageView *mainImageView; 
@property (strong, nonatomic) IBOutlet UIImageView *blurredImageView; 
@property (strong, nonatomic) IBOutlet UILabel *titleLabel; 
@property (strong, nonatomic) IBOutlet UIButton *someButton; 
@property (strong, nonatomic) IBOutlet UILabel *someLabel; 
@property (strong, nonatomic) IBOutlet UILabel *anotherLabel; 
@property (nonatomic) BOOL isBusy; 

@end 

我試着使用實例的[[QRFeedAdCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"feedAdCell"];細胞,但將加載細胞與所有設置爲無屬性的。

我也試着註冊了電池,但測試也失敗:

id mockTableView = [OCMockObject niceMockForClass:[UITableView class]]; 
[mockTableView registerClass:[QRFeedAdCell class] forCellReuseIdentifier:@"feedAdCell"]; 

QRFeedAdCell *cell = [mockTableView dequeueReusableCellWithIdentifier:@"feedAdCell"]; 

XCTAssertNotNil(cell, @""); 
XCTAssertNotNil(cell.mainImageView, @""); 

回答

4

你不能。

,以測試它的唯一方法是實例化故事板,那麼該視圖控制器,然後細胞本身,然後測試您在界面生成器中設置的屬性。

+0

這就是我雖然,我嘗試了更多的東西,但這似乎是唯一的解決方案。謝謝。 – psobko

0

您可以通過編程實例,但你首先需要通過調用

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"]; 

註冊它,那麼你可以做自定義樣式的細胞或任何你通常在這樣的故事板做的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath]; 

     // create and add a label 
     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 155, 21)]; 
     label.text = @"text"; 
     [cell addSubview:label]; 
     ... 
} 
+0

問題是我想測試故事板中設置的內容,我嘗試將代碼添加到問題中,但我無法從故事板訪問任何IBOutlets。 – psobko

+0

是的,我的建議是將問題中的所有IBOutlet設置在自定義單元格中。如果你想重新使用storyboard中的單元格,那麼你只需要使用'QRFeedAdCell * cell = [mockTableView dequeueReusableCellWithIdentifier:@「feedAdCell」];'不用註冊或實例化單元格,如果您在故事板中設置相同的標識符值。 –

+0

無法以這種方式工作。我認爲Rodulf是對的,唯一能讓它工作的方式是實例化故事板。 – psobko

0

這對我來說是基本的工作。我沒有得到viewwillappear運行尚未。
當您在_vc上調用.view時,ViewdidLoad會運行。

@property (nonatomic, strong) Edit_ProfileVC *vc; 
@property (nonatomic, strong) UIView *view; 
@property (nonatomic) NSMutableDictionary *params; 

UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    _vc = [mainSB instantiateViewControllerWithIdentifier:@"EditProfileVC"]; 
    _vc.userData = [[EditUserTableDataSource alloc] initDataSourceWithJSON:self.params]; 
    _view = _vc.view; 
    XCTAssertNotNil(_view, @"the view is not loading properly"); 
相關問題