如何在应用程序测试中正确初始化UITableViewCell?

时间:2013-04-18 15:51:28

标签: ios objective-c unit-testing testing integration-testing

我有UIViewController UITableViewNSMutableArray。该数组将成为UITableView的数据源。我正在尝试为这个视图控制器创建测试并且遇到了困难。设置断点并单步执行,UITableView属性为null。我在viewDidLoad中拨打了viewWillAppearsetUp。这是我的代码和解释:

@implementation IouViewControllerTests

- (void)setUp {
    self.iouViewController = [[IouViewController alloc] init];

    Person *person = [[Person alloc] initWithName:@"Oky" oweItems:[NSMutableArray array]];
    Iou *iouOne = [[Iou alloc] initWithType:@"lend" amount:[NSDecimalNumber one] note:@"1st iou"];
    Iou *iouTwo = [[Iou alloc] initWithType:@"lend" amount:[NSDecimalNumber one] note:@"2nd iou"];
    [person.iouList addObject:iouOne];
    [person.iouList addObject:iouTwo];

    NSMutableArray *persons = [NSMutableArray array];
    [persons addObject:person];
    self.iouViewController.iouTableArray = persons;

    [self.iouViewController viewDidLoad];
    [self.iouViewController viewWillAppear:YES];
    [self.iouViewController.iouTableView setDelegate:self];
}

- (void)tearDown {
    self.iouViewController = nil;
}

-(void)testViewDidLoad {
    STAssertNotNil(self.iouViewController.view, @"iouViewController view should not be nil");
}

- (void)testCustomCell {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    CustomCell *cell = (CustomCell *)[self.iouViewController.iouTableView cellForRowAtIndexPath:indexPath];
    STAssertEqualObjects(cell.personName, @"Oky", @"cell name should be right");
}
  1. Alloc并初始化视图控制器。它使用NIB文件,但该实现隐藏在-init
  2. 创建人物对象并向该人添加2个IOU对象。
  3. 我将该人添加到iouTableArrayUITableViewUITableView的数据源。 viewDidLoad接受一组人并在视图中显示它们。
  4. 致电delegate等,并设置testViewDidLoad(正确?)
  5. 致电testCustomCell传递
  6. 致电NSIndex。创建一个UITableView,用于从CustomCell获取第一行。 UITableViewCellpersonName子类设为UILabel,并将属性cell.personName.text设为cell.personName
  7. 回顾代码,我应该调用null来获取名称字符串。但是,我得到的错误是iouTableView = (UITableView *) 0x00000000UITableView。单步执行,我看到{{1}},我认为这意味着无效。

    我可以做些什么来测试{{1}}中的细胞并使其发挥作用?

    PS:我做了一些阅读,有很多关于嘲弄对象的资料。它们看起来很复杂,但如果必须,我会学到它。是否有苹果推荐的方式去做我想做的事情?

    感谢。

0 个答案:

没有答案