2013-06-18 31 views
0

我不知道如何實現我的模擬UITableView對象正確回答indexPathsForSelectedRows。 在我的應用程序中,用戶可以(在編輯狀態下)選擇表格視圖中的單元格,它表示給定目錄的文件/文件夾。 一旦用戶選擇一個文件夾項目,應該取消選擇以前選擇的文件項目。我的測試(使用OCHamcrest/OCMockito)看起來像這樣。處理UITableView的indexPathsForSelectedRows

- (void)test_tableViewwillSelectRowAtIndexPath_DeselectsPreviouslySelectedCells 
{ 
    // given 
    [given(self.mockTableView.editing) willReturnBool:YES]; 

    // when 
    [self.sut tableView:self.mockTableView willSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:SectionIdFile]]; 
    [self.sut tableView:self.mockTableView willSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:SectionIdFolder]]; 

    // then 
} 

問題是,我可以驗證文件項被選中,但我不能問mockTableView選定的行。有人可以告訴我如何處理?我是否必須自己記錄tableView:selectRowAtIndexPath:animated:scrollPosition:調用,並在tableView被要求提供這些信息時提供正確的答案?

+0

爲什麼你不能要求選定的行?你已經嘗試過'indexPathsForSelectedRows'方法,它返回nil? – geo

+0

詢問selectedRows的mockTableView總是返回nil,但它應該返回第一次調用willSelectRowAtIndexPath時提供的indexPath的數組。 –

回答

0

由於mockTableView無法記錄(如真實UITableView)所選單元格的indexPath,因此您必須確保模擬對象爲該方法返回正確的答案。所以在我的情況下,測試現在看起來像這樣。

- (void)test_tableViewwillSelectRowAtIndexPath_DeselectsPreviouslySelectedCellsForSectionIdFile 
{ 
    // given 
    [given(self.mockTableView.editing) willReturnBool:YES]; 

    NSArray *selectedRows = @[[NSIndexPath indexPathForRow:0 inSection:SectionIdFile], [NSIndexPath indexPathForRow:1 inSection:SectionIdFile]]; 
    [given([self.mockTableView indexPathsForSelectedRows]) willReturn:selectedRows]; 

    // when 
    [self.sut tableView:self.sut.myTableView willSelectRowAtIndexPath:selectedRows[0]]; 
    [self.sut tableView:self.sut.myTableView willSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:SectionIdFolder]]; 

    // then 
    [verify(self.mockTableView) deselectRowAtIndexPath:selectedRows[0] animated:YES]; 
    [verify(self.mockTableView) deselectRowAtIndexPath:selectedRows[1] animated:YES]; 
} 
相關問題