如何在xcode8中为自定义collectionView单元编写单元测试代码

时间:2017-09-05 05:47:35

标签: ios objective-c xcode unit-testing

我是新手编写单元测试代码。任何人都可以告诉我如何为自定义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");
}

1 个答案:

答案 0 :(得分:0)

我不认为Cell非常适合按单元测试进行测试, 我建议改用XCUItest。然后,您将基本断言单元格表示实际数据,并且所有部件都按预期可见。

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