Is it possible to test IBAction?

后端 未结 7 778
深忆病人
深忆病人 2021-01-05 13:50

It is kinda easy to unit test IBOutlets, but how about IBActions? I was trying to find a way how to do it, but without any luck. Is there any way to unit test connection bet

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 14:34

    So, it is probably possible to instantiate a view controller from a storyboard or nib and then do a touch down on a UIButton. However, I wouldn't do this because you are then testing an Apple stock API. Rather, I would test by directly calling the method. For example if you have a method - (IBAction)foo:(id)sender in your view controller and you need to test the logic in that method, I would do something like this:

    MyViewController *viewController = [[MyViewController alloc] initWithNibName:@"NibName" bundle:[NSBundle mainBundle]];
    
    UIButton *sampleButton = [[UIButton alloc] init];
    [sampleButton setTitle:@"Default Storyboard Title" forState:UIControlStateNormal];
    
    [viewController foo:sampleButton];
    
    // Run asserts now on the logic in foo:
    

提交回复
热议问题