0

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しかし、私はそれらを以前にスタブします。

このテストを実際に書く方法についての考えは大歓迎です。

4

2 に答える 2

0

UIKit クラスをテストしているので、次のようなKiwi wiki ページを見ましたか?

UIKit クラス (UILabels など) で操作を実行する仕様は、アプリケーション テスト以外で実行すると、異常にクラッシュする可能性があります。Application Test ターゲットの設定方法の詳細については、Apple の公式ドキュメントを参照してください。

ロジック テストではなく、テスト ターゲットがアプリケーション テストであることを確認したい場合があります。

于 2015-07-01T18:42:22.000 に答える