简体   繁体   中英

How to unit test UIGestureRecognizer subclasses?

Has anyone successfully unit tested subclasses of UIGestureRecognizer? What approach do you use or recommend if so? How do you go about synthesizing events if so?

I am using Cedar and plan on testing custom UIGestureRecognizers by mocking NSTouch objects, bundling them as a set, and making appropriate calls to the various UITouchGestureSubclass points. This unit tests the custom class. The behavior of the parent class I test by calling the action method registered on the UIGR subclass.

I haven't seen a good unit testing solution to this yet and I'm going to need to find one soon so I would love to see or have a better answer.

As @tooluser suggested you might be able to supply "nice" mocks for UITouch objects. Since there's no public constructor for UITouch supplying "real" UITouch instances is tricky and would rely on private API calls. Unfortunately since those touch mocks need to pass through framework code you cannot see or control you have to hope that your mocks actually match all of the expected behaviors of the UITouches they double (including any private behavior we can't see). That makes me nervous.

Alternately you can follow @Sulthan's solution and test your gesture recognizers via UI driving integration tests. This is the approach I have used so far. Use UIAutomation or a third party test framework like Frank or KIF to generate interactions with views and assert that the gesture recognizers fire as expected. That can at least allow you to test the recognizer in isolation (even if not in a true unit test).

I usually want to have some integration tests to verify that interactions between multiple recognizers in custom views are behaving correctly so I don't find this approach too bad but it would be nice to really be able to test drive the development of a recognizer at the unit level (for test execution time if nothing else).

it's not exactly unit testing, but the best method how you can test your user interface is using Automation. Basically you write a javascript and then you run it in Instruments under Automation.

It's not documented very well and there are almost no tutorials. Prepare to be frustrated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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