我不确定如何才能实现我的模拟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被要求提供该信息时提供正确答案?
发布于 2013-06-18 15:58:54
由于mockTableView不能记录(像真正的UITableView一样)所选单元格的索引路径,因此必须确保模拟对象返回该方法的正确答案。因此,在我的例子中,测试现在看起来像这样。
- (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];
}https://stackoverflow.com/questions/17161942
复制相似问题