
我的单元测试基于以下博客(关于如何对视图控制器进行单元测试):
http://yetanotherdevelopersblog.blogspot.co.il/2012/03/how-to-test-storyboard-ios-view.html
在单元测试中,我能够在我正在测试的UIVIEwController中获得指向UICollectionVIEw的指针.这是我的测试的setUp方法中的代码:
storyboard = [UIStoryboard storyboarDWithname:@"MainStoryboard" bundle:nil];vIEwController = [self.storyboard instantiateVIEwControllerWithIDentifIEr:@"MainVIEwController"];[vIEwController performSelectorOnMainThread:@selector(loadVIEw) withObject:nil waitUntilDone:YES];self.collectionVIEw = vIEwController.collectionVIEw
不幸的是,这个UICollectionVIEw中没有单元格(即self.collectionVIEw.visibleCells.count等于0,因此我无法访问单元格并测试数据是我期望的那样),虽然在调试时我可以看到我的应用程序将这些单元格添加到集合视图.
我究竟做错了什么?
任何提示&关于如何在UIVIEwController中单元测试UICollectionVIEw的技巧?
编辑:
在Roshan的帮助之后,我现在可以缩小我的问题范围了.我正在测试一个有UICollectionVIEw的UIVIEwController.在我的单元测试中,我想测试CollectionVIEw中的单元格值是我期待它的.但是,我的VIEwController上没有调用collectionVIEw:cellForItemAtIndexPath:因此当我作为单元测试调用它时,我的单元格不存在.任何的想法?
您应该直接从单元测试中调用这些方法.这只是为您的测试配置合适的环境的问题.
例如,当我为UICollectionVIEw实现TDD时,我创建两个独立的类专门用于实现UICollectionVIEwDataSource和UICollectionVIEwDelegate协议创建一个关注点分离,我可以单独测试这些类到视图控制器本身,虽然我仍然需要初始化查看控制器以设置视图层次结构.
这是一个例子,当然省略了标题和其他小部分代码.
UICollectionVIEwDataSource示例
@implementation CellContentDataSource@synthesize someModelObjectReference = __someModelObjectReference;#pragma mark - UICollectionVIEwDataSource Protocol implementation- (NSInteger) collectionVIEw:(UICollectionVIEw *)collectionVIEw numberOfItemsInSection:(NSInteger)section{ return __someModelObjectReference ? [[__someModelObjectReference modelObjects] count] : 0;}- (UICollectionVIEwCell *) collectionVIEw:(UICollectionVIEw *)collectionVIEw cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static Nsstring * const kCellReuseIDentifer = @"Cell"; UICollectionVIEwCell *cell = [collectionVIEw dequeueReusableCellWithReuseIDentifIEr:kCellReuseIDentifer forIndexPath:indexPath]; ModelObject *modelObject = [__someModelObjectReference modelObjects][[indexPath item]]; /* VarIoUs setter methods on your cell with the model object */ return cell;}@end 单元测试示例
- (voID) testUICollectionVIEwDataSource{ UIStoryBoard *storyboard = [UIStoryboard storyboarDWithname:@"MainStoryboard" bundle:nil]; MainVIEwController *vIEwController = [storyboard instantiateVIEwControllerWithIDentifIEr:@"MainVIEwController"]; [vIEwController vIEw]; // Loads the vIEw hIErarchy // Using Ocmock to mock the returning of the model objects from the model object reference. // The helper method referenced builds an array of test objects for the collection vIEw to reference ID modelObjectMock = [OcmockObject mockForClass:[SomeModelObjectReference class]]; [[[modelObjectMock stub] andReturn:[self buildModelObjectsForTest]] modelObjects]; CellContentDataSource *dataSource = [CellContentDataSource new]; dataSource.someModelObjectReference = modelObjectMock; vIEwController.collectionVIEw.dataSource = dataSource; // Here we call the data source method directly UICollectionVIEwCell *cell = [dataSource collectionVIEw:vIEwController.collectionVIEw cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; XCTAssertNotNil(cell,@"Cell should not be nil"); // Assert your cells contents here based on your test model objects} 总结 以上是内存溢出为你收集整理的ios – 如何在UIViewController中对UICollectionView进行单元测试全部内容,希望文章能够帮你解决ios – 如何在UIViewController中对UICollectionView进行单元测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)