Testing gestures and actions. Unit testing view’s actions in Swift… | by Giovani Pereira | Mac O’Clock | Jun, 2021

Picture by Brooke Lark on Unsplash

Unit testing view’s actions in Swift with out a host app

Giovani Pereira

In the event you love unit exams (both by genuinely liking them or by understanding and fearing the problems that include the dearth of exams) chances are you’ll all the time get on a psychological impasse when serious about testing your views.

What’s testing a view? Do snapshot exams depend as testing a view? Do I want to vary the visibility of my elements so I can higher check them? We won’t be answering these questions right here in the present day. However we can be having a look into how we are able to unitarily check our view’s gestures and buttons to make sure that each motion carried out will set off the specified impact.

Lets say a easy state of affairs, you wish to check the conduct of the view:

  • If I faucet this button, the cell state modifications;
  • If I do that gesture, this delegate is named;

Which each is sensible. However the challenge right here is, how can I simulate these person interactions as tapping a button, or performing a gesture throughout a unit check?

In case your button’s reference is non-public, or non-visible for some purpose, chances are you’ll use Reflection to get its reference. Verify: Using Mirror to test references in swift.

The strategy sendActions permits us to ship any UIControl.Occasion to a UIControl — which UIButtons extends from — and set off your actions.

Performing a selector

And tadaaa! With two weird-looking strains of code, we are able to obtain the identical impact as if we have been utilizing a bunch app and the sendActions.

Simulating a gesture

This one is a little bit bit extra difficult, however we are able to entry the Gesture Recognizers of the view by way of the property gestureRecognizers, which provides us an elective array of UIGestureRecognizers.

Then, we are able to discover, inside this array, the gesture we wish to carry out (contemplating you might have a couple of gesture on the identical view). And now could be the half it will get bizarre: We have to get the goal from the Gesture Recognizer and extract the Selector String out of it.

With the Selector String in hand, we are able to now ask our view to carry out the Selector and assert the outcomes we expect.

Reusing our answer

I prefer to create extensions for the XCTestCase class with each these carry out strategies, so I can simply reuse them whereas testing my views.

With these two strategies, we are able to now simplify loads our check circumstances:

And use them to check all types of button’s actions and gestures.

Cautious with unknown selectors

  • Our buttons and gestures selectors are actually working;
  • When carried out, they set off the anticipated actions;

However, if for some purpose you attempt to carry out a selector which does not exist, don’t concern, it should merely fail your check telling you that you simply tried to carry out an unexistent selector.

Failed check because of unrecognized selector

Which can also be a superb factor as a result of your button/gesture was not set appropriately and also you caught it on the check!

That is all people! Unit check all the best way.

#Testing #gestures #actions #Unit #testing #views #actions #Swift #Giovani #Pereira #Mac #OClock #Jun



What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Monitoring and Optimizing efficiency in PostgreSQL and Google Cloud SQL | by Weiyuan | Jun, 2021

Disband HSOC and Hearth Jeff Kositsky | by Road Sheet | Jun, 2021