Is it possible to test IBAction?

后端 未结 7 818
深忆病人
深忆病人 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:14

    I did it using OCMock, like this:

    MyViewController *mainView =  [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    [mainView view];
    
    id mock =  [OCMockObject partialMockForObject:mainView];
    
    //testButtonPressed IBAction should be triggered
    [[mock expect] testButtonPressed:[OCMArg any]];
    
    //simulate button press 
    [mainView.testButton sendActionsForControlEvents: UIControlEventTouchUpInside];
    
    [mock verify];
    

    If IBAction is not connected, the test will fail with error "expected method was not invoked".

提交回复
热议问题