处理UITableView的indexPathsForSelectedRows

时间:2013-06-18 06:29:23

标签: ios tdd ocmockito

我不确定如何实现我的模拟UITableView对象正确回答indexPathsForSelectedRows。 在我的应用程序中,用户可以(在编辑状态下)在表视图中选择单元格,该视图表示给定目录的文件/文件夹。 一旦用户选择了文件夹项,就应该取消选择先前选择的文件项。我的测试(使用OCHamcrest / OCMockito)看起来像这样。

- (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提供正确答案时提供正确答案?

1 个答案:

答案 0 :(得分: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];
}