相关疑难解决方法(0)

使用storyBoards时单元测试cellForRowAtIndexPath

如果我从故事板中的标识符中出列单元格,那么在单元测试方式中如何调用cellForRowAtIndexPath而不是单元格为零?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCell];

    cell.guestNameText.text = self.details.guestName;

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

不工作,在调用dequeReusableCell并且cell为nil之后设置一个断点:

ETA:更新的工作代码以通过测试:

- (void)setUp {

    [super setUp];
    _detailVC_SUT = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]
     instantiateViewControllerWithIdentifier:kDetailsVC];
    _myService = [OCMockObject niceMockForClass:[MyService class]];
    _detailVC_SUT.service = _myService;
}


- (void)test_queryForDetailsSucceeded_should_set_cell_text_fields {

    [_detailVC_SUT view]; // <--- Need to load the view for this to work
    Details *details = [DetailsBuilder buildStubDetails];
    [_detailVC_SUT queryForDetailsSucceededWithDetails:details];

    [self getFirstCellForGuestName];
}

- (void)getFirstCellForGuestName {

    MyCustomTableViewCell *guestNameCell = (MyCustomTableViewCell*)[_detailVC_SUT tableView:_detailVC_SUT.detailsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

    expect(guestNameCell.guestNameText.text).to.equal(@"Mark");
}
Run Code Online (Sandbox Code Playgroud)

iphone unit-testing objective-c storyboard ios

4
推荐指数
1
解决办法
2498
查看次数

标签 统计

ios ×1

iphone ×1

objective-c ×1

storyboard ×1

unit-testing ×1