繁体   English   中英

测试prepareForSegue是否符合预期的方法

[英]a way to test if prepareForSegue does what it's supposed to

我不确定如何在单元测试中执行segue,以便prepareForSegue运行。

尽我所能用单元测试覆盖所有内容,这就是我正在尝试的内容:

- (void)setUp {

    [super setUp];
    _mainVC = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]
                      instantiateViewControllerWithIdentifier:kMainVC];
    _detailsVC = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]
                      instantiateViewControllerWithIdentifier:kDetailsVC];
}

- (void)test_prepareForSegue_will_set_confNumber_on_DetailsVC {

   UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:kDetailsSegueId source:_mainVC destination:_detailsVC performHandler:^{
        // do anything here ???
    }];

    [segue perform];
    expect(_detailsVC.textFieldStr).to.equal(@"123456abc");
}

我注意到,当我放入断点时,主视图控制器中的prepareForSegue永远不会被击中。

DUH。

只需在正在测试的ViewController上调用prepareForSegue :)

UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:kDetailsSegueId source:_mainVC destination:_detailsVC performHandler:^{
        // do nothing here
    }];

[_mainVC prepareForSegue:segue sender:self];
expect(_detailsVC.confNumber).to.equal(@"123456abc");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM