2017-09-05 79 views
0

我是新编写的单元测试代码。任何人都可以告诉我如何编写自定义collectionView单元格的单元测试代码。这是我试过的代码,它显示单元格是零。如何在xcode8中编写自定义collectionView单元的单元测试代码

#import <XCTest/XCTest.h> 
#import "CollectionViewCell.h" 

@interface CollectionViewCellTests : XCTestCase 
{ 
     UICollectionView *testCollectionView; 
     CollectionViewCell *customCell; 

} 
@end 

@implementation CollectionViewCellTests 

- (void)setUp { 
    [super setUp]; 
     // Put setup code here. This method is called before the invocation of each test method in the class. 

     //[testCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"]; 
     // [testCollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; 
} 

- (void)tearDown { 
     // Put teardown code here. This method is called after the invocation of each test method in the class. 
     [super tearDown]; 
} 

- (void)testCustomCollectionViewCell { 

     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
     customCell = (CollectionViewCell *)[testCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 

     XCTAssertNotNil(customCell, @"No Custom Cell Available"); 
} 

回答

0

我不认为一个细胞是相当适合作为测试单位测试, 我推荐使用XCUItest代替。然后你会基本断言该单元格代表实际数据,并且所有部分都如预期那样可见。

在设置部分中,您将填充数据,然后实际测试会断言该视图已正确显示。

+0

但单元格显示为零 –

+0

确保您已将您的tableview设置为有一些数据,这需要在您的设置部分完成。所以你必须公开你的类数据容器,以便你可以从你的uitest中添加一些数据。你可以在你的tableView中设置断点:numberOfRowsInSection正在被调用,你返回一个正确的金额,通常是你的Data.count – Wayne

+0

我实际上建议你在XCUITests上观看一些简短的视频,你可以快速记录一些用户操作,并从那里完善你的测试。 – Wayne

相关问题