Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to instantiate a prototype UITableViewCell from a storyboard for testing?

I'm using a combination of XCTest and OCMock for testing an iOS application. I'm using a UITableViewController to present a series of prototype cells which I would like to write some tests for. The cell itself is in a storyboard so I don't believe I can instantiate it from a nib.

Is the only option to use the viewController which uses the cell to instantiate it?

The custom cell class I'm using has a number of 'IBOutlets' connected to the prototype on the storyboard. The cell class looks something like this:

@interface QRFeedAdCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *mainImageView;
@property (strong, nonatomic) IBOutlet UIImageView *blurredImageView;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UIButton *someButton;
@property (strong, nonatomic) IBOutlet UILabel *someLabel;
@property (strong, nonatomic) IBOutlet UILabel *anotherLabel;
@property (nonatomic) BOOL isBusy;

@end

I've tried instantiating the cell using [[QRFeedAdCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"feedAdCell"]; but that will load the cell with all of the properties set to nil.

I've also tried registering the cell, but the tests also fail:

 id mockTableView = [OCMockObject niceMockForClass:[UITableView class]];
[mockTableView registerClass:[QRFeedAdCell class] forCellReuseIdentifier:@"feedAdCell"];

QRFeedAdCell *cell = [mockTableView dequeueReusableCellWithIdentifier:@"feedAdCell"];

XCTAssertNotNil(cell, @"");
XCTAssertNotNil(cell.mainImageView, @"");
like image 429
psobko Avatar asked Nov 14 '25 13:11

psobko


1 Answers

You can't.

The only way to test it is to instantiate the storyboard, then the view controller, then the cell itself, and then test the properties you set in the Interface Builder.

like image 103
Rudolf Adamkovič Avatar answered Nov 17 '25 09:11

Rudolf Adamkovič