Fast Unit Testing IBAction

I have a UIViewController that contains every UITextField , UIButton and UILabel . I put something in the UITextField , click the button and the string is now capitalized in the UILabel .

My question is, how do I set up IBAction in Swift for unit tests? If there is no way to test an action in Swift, what else can I do to test it?

+3


source to share


3 answers


In the world of unit testing, UI testing can be the most difficult task. So what you can do is check everything that is available to you from the API.

You cannot switch an action or event like a phone user. So you have to programmatically toggle actions or events to check on this. You will also have to programmatically initialize the UI elements yourself.



IBAction is just an indicator to tell UIStoryboard that it is a connector method, you can ignore it and treat it as a normal method.

+1


source


Unit tests are not intended to test the visual interface. Did this to test your code.



You can use Apple UI Automation tool or other external tools to test the interface .

0


source


Personally, I tried to make a mock for the class and post the following code:

controllerMock.btnNoResults.sendActionsForControlEvents(UIControlEvents.TouchUpInside)

      

The problem is that the call is not being made for the mock, so the test is useless.

The same technique done in Objective-C with OCMock works great, so I use quick tests and Objective-C tests.

To set up an Objective-C test for fast source testing, I basically need to do the following:

  • Add OCMock to my test project
  • Create an Objective-C test. He will ask me how the headline usually lights up.
  • Import "OCMock.h"
  • VERY IMPORTANT: import the header to get access to the quick functions of my project. Usually this header "ProjectName_Tests-Swift.h"
  • Use the following code:

    -(void) testSeeMoreInformationAction{
        id mock = [OCMockObject partialMockForObject:pController];
        [[mock expect]seeMoreInformation];
    
        [pController.btnSeeMoreDetails sendActionsForControlEvents:UIControlEventTouchUpInside];
    
        [mock verify];
    }
    
          

Like my test. Also the good thing about mixing Objective-C with Swift and adding OCMock is that you can now test many other things more easily. Please note that when using OCMock and Swift, it only works with iOS SDK functions, it will not work with your swift functions.

Here is an example of another OCMock test very useful with AlertView:

- (void)testLoginShowsAlertViewWhenNoUserNameAndPassword{
    pController.txtUserName.text = @"";
    pController.txtPassword.text = @"";
    id mock = [OCMockObject partialMockForObject:pController];
    [[mock expect]presentViewController:OCMOCK_ANY animated:YES completion:nil];

    [pController loginAction];

    [mock verify];
}

      

Please do not hesitate to contact me if you need further help testing devices. I think this is one of the most interesting parts of fast development.

0


source







All Articles