我需要用Kiwi测试一个UICollectionViewFlowLayout子类,
我已经正确地模拟了UICollectionView的委托和dataSource,但我仍然遇到了一些问题。
如果指定的项目大小为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
});
这是我在没有测试时运行应用程序时看到的内容: