當測試從故事板中加載的UITableViewController子類時,cellForRowAtIndexPath將返回零,除非我:A)發送它viewWillAppear:YES或B)獲取tableView.numberOfSections。爲什麼我必須調用viewWillAppear來獲取cellForRowAtIndexPath?
這是爲什麼?爲什麼不訪問視圖或發送主線程上的loadView選擇器來完成這項工作?
SpecBegin(CustomerViewController)
describe(@"CustomerViewController", ^{
__block CustomerViewController *_vc;
beforeEach(^{
NSLog(@"Before Each");
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"Storyboard_Pad" bundle:nil];
_vc = (CustomerViewController *)[mainstoryboard instantiateViewControllerWithIdentifier:@"customer"];
});
it(@"the first cell should be the fullName cell", ^{
// Following line is needed to make the following tests pass.
[_vc viewWillAppear:YES];
UITableViewCell *cell = [_vc.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
expect(cell.reuseIdentifier).to.equal(@"fullName");
});
});
SpecEnd
是什麼在你viewWillAppear中的方法?該方法是否可以設置表視圖的數據源? –
我甚至不確定你發佈的代碼的含義。什麼是「describe」,「it」和「expect」的函數?他們在做什麼? – Macondo2Seattle
@BlackRider他正在嘗試單元測試UI。瑞恩,我強烈建議測試你的模型,而不是消耗它的UI。 –