3

我需要用 Kiwi 测试一个 UICollectionViewFlowLayout 子类,

我已经正确地模拟了 UICollectionView 的委托和数据源,但我仍然遇到一些问题。

使用指定的项目大小CGSize(200.0f, 200.0f),我应该在屏幕上获得 5 个项目,但由于某种原因,最后一行返回的属性数组确实返回 10 个属性,这意味着有 10 个可见单元格。

这里会发生什么?如果我的布局按预期工作,屏幕上总是有 5 个元素:

这就是我到目前为止所拥有的(阅读评论),而且它大部分都有效。

describe(@"LineLayout", ^{
    __block UICollectionView *collectionView;
    __block HICollectionViewLineLayout *layout;
    __block const CGRect windowFrame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
    __block const CGSize itemSize = CGSizeMake(200.0f, 200.0f);

    // Create a Collection View that uses a LineLayout and set the datasource delegate
    // before each test
    beforeEach(^{
        layout = [[HICollectionViewLineLayout alloc] init];
        collectionView = [[UICollectionView alloc] initWithFrame:windowFrame collectionViewLayout:layout];

        // Mock the UILineLayout data source
        id delegateMock = [KWMock mockForProtocol:@protocol(UICollectionViewDelegateFlowLayout)];
        [[delegateMock should] conformToProtocol:@protocol(UICollectionViewDelegateFlowLayout)];
        [delegateMock stub:@selector(collectionView:layout:sizeForItemAtIndexPath:) andReturn:theValue(itemSize)];
        [delegateMock stub:@selector(collectionView:layout:insetForItemAtIndex:) andReturn:theValue(UIEdgeInsetsZero)];

        // Mock the UICollection View dataSource
        id dataSourceMock = [KWMock mockForProtocol:@protocol(UICollectionViewDataSource)];
        [[dataSourceMock should] conformToProtocol:@protocol(UICollectionViewDataSource)];
        [dataSourceMock stub:@selector(numberOfSectionsInCollectionView:) andReturn:theValue(1)];
        [dataSourceMock stub:@selector(collectionView:numberOfItemsInSection:) andReturn:theValue(10)];

        // Set the delegate and the data source
        collectionView.delegate = delegateMock;
        collectionView.dataSource = dataSourceMock;

        // Reload the collectionView Data
        [collectionView reloadData];
    });

    it(@"Should properly identify central element when cell number is not even", ^{
        NSArray *attributes = [layout layoutAttributesForElementsInRect:windowFrame];

        // test that [attributes count] == 5
    });

这是我在没有测试的情况下运行应用程序时看到的:

在此处输入图像描述

4

0 回答 0