Question

I'm not sure how I can implement that my mock UITableView object answers correctly for indexPathsForSelectedRows. In my App the user can (in editing state) select cells in a table view, which represents the files/folders of a given directory. Once the user selects a folder item the previously selected files items should be deselected. My test (using OCHamcrest/OCMockito) looks like this.

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

The problem is that I can verify that the file item is selected but I can't ask the the mockTableView for its selected rows. Could somebody tell me how to handle that? Do I have to record the tableView:selectRowAtIndexPath:animated:scrollPosition: calls myself and provide the correct answer when the tableView is asked for that information?

Was it helpful?

Solution

As the mockTableView can not record (like the real UITableView) the indexPath's of the selected cell, you have to make sure that the mock object returns the correct answer for that method. So in my case the test looks now like this.

- (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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top