Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing View Controllers --iPhone

My question is 2-fold. 1. Can I use OCUnit to test View Controllers. If so, how should I do it? If not, is there another Testing Kit I can use?

like image 806
Adrian Sarli Avatar asked Jun 10 '26 12:06

Adrian Sarli


1 Answers

You definitely can. Say you had a UITableViewController, and you wanted to make sure that it had 2 sections with 5 rows each; that is easily done in a test method like so:

- (void) testTableHasCorrectRowsAndSections
{
  id tableViewController = [[[YourTableViewControllerSubclass alloc] init] autorelease];

  STAssertEquals(2,[tableViewController numberOfSectionsInTableView:nil],@"");
  STAssertEquals(5,[tableViewController tableView:nil numberOfRowsInSection:0],@"");
  STAssertEquals(5,[tableViewController tableView:nil numberOfRowsInSection:1],@"");
}

I would also recommend also utilizing OCMock to help you with testing your controllers. you can easily mock a view and ensure that your controller is interacting with it properly.

like image 167
Kevlar Avatar answered Jun 12 '26 11:06

Kevlar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!