2017-09-05 80 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

相關問題