OGeek|极客世界-中国程序员成长平台

标题: ios - 有没有办法从 Storyboard 中实例化原型(prototype) UITableViewCell 进行测试? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:57
标题: ios - 有没有办法从 Storyboard 中实例化原型(prototype) UITableViewCell 进行测试?

我正在使用 XCTestOCMock 的组合来测试 iOS 应用程序。我正在使用 UITableViewController 来呈现一系列我想为其编写一些测试的原型(prototype)单元。单元格本身位于 Storyboard 中,所以我不相信我可以从 Nib 实例化它。

是使用 viewController 的唯一选项,它使用单元格来实例化它?

我正在使用的自定义单元类有许多“IBOutlets”连接到 Storyboard 上的原型(prototype)。单元类看起来像这样:

@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

我尝试使用 [[QRFeedAdCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier"feedAdCell"]; 实例化单元格,但这会加载所有属性设置为 nil 的单元格。

我也尝试过注册单元,但测试也失败了:

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

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

XCTAssertNotNil(cell, @"");
XCTAssertNotNil(cell.mainImageView, @"");



Best Answer-推荐答案


你不能。

测试它的唯一方法是实例化 Storyboard ,然后是 View Controller ,然后是单元格本身,然后测试您在 Interface Builder 中设置的属性。

关于ios - 有没有办法从 Storyboard 中实例化原型(prototype) UITableViewCell 进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21145211/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4