I need to test a UICollectionViewFlowLayout subclass with Kiwi,
I have correctly mocked up the delegate and the dataSource for a UICollectionView, but I'm still having some issues.
With the specified item size of CGSize(200.0f, 200.0f)
, I should be getting 5 items on the screen, but for some reason the attributes array that is returned at the last line does return 10 attributes, so that means that there are 10 visible cells.
What could be going on here? If my layout works as expected there's always 5 elements on the screen:
This is what I have so far (read the comments), and it mostly works.
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
});
This is what I see when I just run the app with no tests: