OCHamcrest Snippets

TDD TableView

The following steps demonstrate can TDD can be used to add a TableView to a view. Each test can be added sequentially, and the corresponding product code created to pass each test.

Setup

#import 

#define HC_SHORTHAND
#import 

#define MOCKITO_SHORTHAND
#import 

#import "MyViewController.h"

@interface MyViewControllerTests
@property (nonatomic, strong) MyViewController *testObj;
@end

@interface MyViewControllerTests

- (void)setUp
{
    self.testObj = [[EngineerViewController alloc] initWithRect:CGRectZero];
    [self forceLoadingOfTheView];
}

- (void)forceLoadingOfTheView
{
    assertThat(self.testObj.view, notNilValue());
}

- (void)tearDown
{
    self.testObj = nil;
}

Verifying properties

- (void)testMyViewControllerInstantiates
{
    assertThat(self.testObj, notNilValue());
}

- (void)testThatTableViewContainerPropertyExists
{
    assertThatBool([self.testObj respondsToSelector:@selector(tableViewContainer)], equalToBool(YES));
}

- (void)testThatTableViewContainerPropertyIsWritable
{
    assertThatBool([self.testObj respondsToSelector:@selector(setTableViewContainer:)], equalToBool(YES));
}

Verifying Container and Table Created

- (void)testTableContainerViewCreated
{
    assertThat(self.testObj.tableViewContainer, notNilValue());
}

- (void)testTableViewCreated
{
    assertThat(self.testObj.tableView, notNilValue());
}

TableView DataSource and Delegate

- (void)testThatObjectConformsToUITableViewDataSource
{
    assertThat(self.testObj, conformsTo(@protocol(UITableViewDataSource)));
}

- (void)testThatTableViewDataSourceIsConnected
{
    assertThat(self.testObj.tableView.dataSource, notNilValue());
}

- (void)testThatObjectConformsToUITableViewDelegate
{
    assertThat(self.testObj, conformsTo(@protocol(UITableViewDelegate)));
}

- (void)testTableViewDelegateIsConnected
{
    assertThat(self.testObj.tableView.delegate, notNilValue());
}

View Hierarchy

- (void)testTableViewIsSubviewOfContainerView
{
    NSArray *subviews = self.testObj.tableContainerView.subviews;
    assertThat(subviews, hasItem(self.testObj.tableView));
}

TableViewNumberOfRowsInSection

- (void)testTableViewNumberOfRowsInSection
{
    int expectedNumberOfRows = 3;
    assertThatInt([self.testObj tableView:self.testObj.projectsTableView numberOfRowsInSection:0], 
                  equalToInt(expectedNumberOfRows));
}

Row Height

- (void)testTableViewHeightForRowAtIndexPath
{
    CGFloat expectedHeight = 44.0;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    CGFloat height = [self.testObj tableView:nil heightForRowAtIndexPath:indexPath];
    assertThatFloat(height, equalToFloat(expectedHeight));
}

CellForRowAtIndexPath

- (void)testTableViewCellForRowAtIndexPathReturnsCellClass
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self.testObj tableView:self.testObj.tableView cellForRowAtIndexPath:indexPath];
    assertThat(cell, instanceOf([MyCell class]));
}

- (void)testTableViewCellForRowAtIndexPathReturnsCellWithReuseIdentifier
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self.testObj tableView:self.testObj.tableView cellForRowAtIndexPath:indexPath];
    assertThat(cell.reuseIdentifier, equalTo(@"MyCell"));
}

- (void)testCellForRowAtIndexPathReusesCells
{
    UITableView *mockTable = mock([UITableView class]);
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.testObj tableView:mockTable cellForRowAtIndexPath:indexPath];
    [verify(mockTable) dequeueReusableCellWithIdentifier:@"ProjectCell"];
}

To be continued…

Leave a Reply