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
しかし、私はそれらを以前にスタブします。
このテストを実際に書く方法についての考えは大歓迎です。