如果我要从故事板中的标识符中取出单元格,如何以单元测试的方式调用 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");
}
这是我测试表视图及其单元格的方法。这里的关键是调用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)
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)