Ios 是否有任何方法可以从故事板实例化原型UITableViewCell进行测试?

Ios 是否有任何方法可以从故事板实例化原型UITableViewCell进行测试?,ios,objective-c,uitableview,ocmock,xctest,Ios,Objective C,Uitableview,Ocmock,Xctest,我使用XCTest和OCMock的组合来测试iOS应用程序。我正在使用UITableViewController来展示一系列原型单元,我想为它们编写一些测试。单元本身在故事板中,所以我不相信我可以从nib中实例化它 是使用viewController的唯一选项,它使用单元格来实例化它吗 我使用的自定义单元类有许多“IBOutlets”连接到故事板上的原型。cell类的外观如下所示: @interface QRFeedAdCell : UITableViewCell @property (str

我使用XCTest和OCMock的组合来测试iOS应用程序。我正在使用UITableViewController来展示一系列原型单元,我想为它们编写一些测试。单元本身在故事板中,所以我不相信我可以从nib中实例化它

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

我使用的自定义单元类有许多“IBOutlets”连接到故事板上的原型。cell类的外观如下所示:

@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重用标识符:@feedAdCell]实例化该单元格;但这将加载所有属性都设置为nil的单元格

我也尝试过注册手机,但测试也失败了:

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

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

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

您可以通过编程方式实例化,但首先需要通过调用

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
然后,您可以对单元格进行自定义样式设置,或者像这样在故事板中通常执行的任何操作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];

        // create and add a label
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 155, 21)];
        label.text = @"text";
        [cell addSubview:label];
        ...
}

您可以通过编程方式实例化,但首先需要通过调用

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
然后,您可以对单元格进行自定义样式设置,或者像这样在故事板中通常执行的任何操作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];

        // create and add a label
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 155, 21)];
        label.text = @"text";
        [cell addSubview:label];
        ...
}
你不能

测试它的唯一方法是实例化情节提要、视图控制器、单元格本身,然后测试您在界面生成器中设置的属性。

您不能


测试它的唯一方法是实例化故事板,然后实例化视图控制器,然后实例化单元格本身,然后测试您在Interface Builder中设置的属性。

这对我来说是一种基本的方法。我还没看到你要跑的样子。 ViewdidLoad在调用时运行

@property (nonatomic, strong) Edit_ProfileVC *vc;
@property (nonatomic, strong) UIView *view;
@property (nonatomic) NSMutableDictionary *params;

 UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    _vc = [mainSB instantiateViewControllerWithIdentifier:@"EditProfileVC"];
    _vc.userData = [[EditUserTableDataSource alloc] initDataSourceWithJSON:self.params];
    _view = _vc.view;
    XCTAssertNotNil(_view, @"the view is not loading properly");

这对我来说是一种基本的方式。我还没看到你要跑的样子。 ViewdidLoad在调用时运行

@property (nonatomic, strong) Edit_ProfileVC *vc;
@property (nonatomic, strong) UIView *view;
@property (nonatomic) NSMutableDictionary *params;

 UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    _vc = [mainSB instantiateViewControllerWithIdentifier:@"EditProfileVC"];
    _vc.userData = [[EditUserTableDataSource alloc] initDataSourceWithJSON:self.params];
    _view = _vc.view;
    XCTAssertNotNil(_view, @"the view is not loading properly");

问题是,我想测试故事板中设置了什么,我尝试了添加到问题中的代码,但我无法从故事板访问任何IBoutlet。是的,我的建议是将自定义单元格中的所有IBoutlet设置为有问题的。根据修改后的问题中的附加信息,如果要从故事板中重复使用单元格,则只需使用QRFeedAdCell*单元格=[mockTableView dequeueReusableCellWithIdentifier:@feedAdCell];如果在情节提要中设置相同的标识符值,则不注册或实例化单元格。无法以这种方式使其工作。我认为Rodulf是对的,我唯一能让它工作的方法就是实例化故事板。问题是我想测试故事板中设置的内容,我尝试了添加到问题中的代码,但我无法从故事板访问任何IBoutlet。是的,我的建议是有问题地设置自定义单元格中的所有IBOutlet。根据修改后的问题中的附加信息,如果要从故事板中重复使用单元格,则只需使用QRFeedAdCell*单元格=[mockTableView dequeueReusableCellWithIdentifier:@feedAdCell];如果在情节提要中设置相同的标识符值,则不注册或实例化单元格。无法以这种方式使其工作。我认为Rodulf是对的,我唯一能让它工作的方法就是实例化故事板。这就是我想的,我尝试了更多的东西,但这似乎是唯一的解决方案。谢谢。这就是我想的,我尝试了更多的东西,但这似乎是唯一的解决办法。谢谢