使用storyBoards时单元测试cellForRowAtIndexPath [英] unit test cellForRowAtIndexPath when using storyBoards

查看:95
本文介绍了使用storyBoards时单元测试cellForRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从故事板中的标识符中取出单元格,那么在单元测试方式中如何调用cellForRowAtIndexPath并且不将单元格设为nil?

If I'm dequeuing a cell from an identifier in a storyboard, how in a unit testing way can I call cellForRowAtIndexPath and not have the cell be nil?

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

    MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCell];

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

    return cell;
}

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

Not working, put a break point above after dequeReusableCell is called and cell is 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");
}


推荐答案

这是我测试表的方法观点和他们的细胞。这里的关键是在视图控制器上调用 beginAppearanceTransition ,以便从故事板加载它。

Here is how I test table views and their cells. The key here is to call beginAppearanceTransition on view controller in order to load it from the storyboard.

class MyTests: XCTestCase {
  var viewController: UIViewController!

  override func setUp() {
    super.setUp()

    let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
    viewController = storyboard.instantiateViewControllerWithIdentifier("myViewControllerId")
    viewController.beginAppearanceTransition(true, animated: false)
  }

  override func tearDown() {
    super.tearDown()

    viewController.endAppearanceTransition()
  }


  func testShowItemsFromNetwork() {
    //
    // Load the table view here ...
    //

    let tableView = viewController.tableView

    // Check the number of table rows

    XCTAssertEqual(3, tableView.dataSource?.tableView(tableView, numberOfRowsInSection: 0))

    // Check label text of the cell in the first row

    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    let cell = tableView.dataSource?.tableView(tableView, cellForRowAtIndexPath: indexPath)
    XCTAssertEqual("Test cell title", cell!.textLabel!.text)
  }
}

这篇关于使用storyBoards时单元测试cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆