When implementing TTD on iOS, try not to rely on a system that invokes delegate and data source methods.
You should call these methods directly from your unit tests. It's just a matter of setting up the right environment for your tests.
For example, when I implement TDD for UICollectionView, I create two separate classes specifically for implementing the UICollectionViewDataSource and UICollectionViewDelegate protocols, which create a separation of problems, and I can unit test these classes separately access the view controller itself, although I still need to initialize the view controller to configure hierarchies of representations.
Here's an example, headers and other minor code snippets are missing, of course.
UICollectionViewDataSource example
@implementation CellContentDataSource @synthesize someModelObjectReference = __someModelObjectReference; #pragma mark - UICollectionViewDataSource Protocol implementation - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return __someModelObjectReference ? [[__someModelObjectReference modelObjects] count] : 0; } - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString * const kCellReuseIdentifer = @"Cell"; UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifer forIndexPath:indexPath]; ModelObject *modelObject = [__someModelObjectReference modelObjects][[indexPath item]]; /* Various setter methods on your cell with the model object */ return cell; } @end
Unit Test Example
- (void) testUICollectionViewDataSource { UIStoryBoard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; MainViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"MainViewController"]; [viewController view];
source share