2013-06-18 1 views
0

에 대해 내 모의목 UITableView 개체가 올바르게 대답 할 수 있는지 잘 모르겠습니다. 내 응용 프로그램에서 사용자는 (편집 상태에서) 지정된 디렉토리의 파일/폴더를 나타내는 테이블보기에서 셀을 선택할 수 있습니다. 사용자가 폴더 항목을 선택하면 이전에 선택한 파일 항목의 선택을 취소해야합니다. 내 테스트 (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가 선택한 셀의 indexPath를 (실제 UITableView처럼) 기록 할 수 없으므로 mock 객체가 해당 메소드에 대한 올바른 대답을 반환하는지 확인해야합니다. 그래서 제 경우에는 테스트가 이렇게 보입니다.

- (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]; 
}