我正在使用 Kiwi 为应用程序编写测试。我正在尝试验证从返回的单元格tableView:cellForRowAtIndexPath:
在调用后是否设置了正确的值。我已经做了很多不同的变化,但没有运气:
describe(@"tableView:cellForRowAtIndexPath:", ^{
it(@"Should return a cell with proper label values",
^{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
id mockTableView = [UITableView mock];
id mockCell = [UITableViewCell mock];
[mockTableView stub:@selector(dequeueReusableCellWithIdentifier:forIndexPath:) andReturn:mockCell withArguments:any(), indexPath];
[mockCell stub:@selector(label1)
andReturn:[[UILabel alloc] init]];
[mockCell stub:@selector(label2)
andReturn:[UILabel alloc]];
CustomTableViewCell *cell = (CustomTableViewCell *)
[dataSource tableView:mockTableView
cellForRowAtIndexPath:indexPath];
[[cell.label1.text should]
equal:@"abc"];
[[cell.label2.text should] equal:@"xyz"];
});
});
实际方法如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:kCustomCellReuseIdentifier
forIndexPath:indexPath];
CustomObject *obj = [self objAtIndexPath:indexPath];
[self setupCell:cell withObj:obj];
return cell;
}
- (void)setupCell:(
CustomTableViewCell *)cell withObj:(CustomObject *)obj
{
cell.label1.text = @"abc";
cell.label2.text = @"xyz";
}
它似乎被抓住了cell.label1
——nil
然而,我之前确实存根存根。
欢迎任何关于如何实际编写此测试的想法。