使用storyBoards时单元测试cellForRowAtIndexPath

时间:2013-07-25 17:17:06

标签: iphone ios objective-c unit-testing storyboard

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

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

    MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCell];

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

    return cell;
}

不工作,在调用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");
}

2 个答案:

答案 0 :(得分:8)

以下是我测试表视图及其单元格的方法。这里的关键是在视图控制器上调用beginAppearanceTransition,以便从故事板加载它。

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)
  }
}

答案 1 :(得分:0)

表视图在尝试调用cellForRow(at :)时有一个CGRect.zero框架。结果,cellForRow(at :)返回nil。为了测试它,我在你的测试函数中做了以下几点:

func testCellForRow_AtZero_ReturnsTaskCell() {
    let mockTableView = UITableView(frame: CGRect(x: 0, y: 0, width: 320, height: 480), style: .plain)

     mockTableView.dataSource = dataSource
     mockTableView.register(YourTableViewCell.self, forCellReuseIdentifier: String(describing: YourTableViewCell.self))

     //The reload is also necessary
     mockTableView.reloadData()

     let cell = mockTableView.cellForRow(at: IndexPath(row: 0, section: 0))

     XCTAssertTrue(cell is YourTableViewCell)
}

不要忘记在dataSource中你应该有一个Indexpath的值(row:0,section:0)。否则测试会失败。