简体   繁体   English

如何对UIGestureRecognizer子类进行单元测试?

[英]How to unit test UIGestureRecognizer subclasses?

Has anyone successfully unit tested subclasses of UIGestureRecognizer? 有没有人成功测试过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. 我正在使用Cedar并计划通过模拟NSTouch对象来测试自定义UIGestureRecognizers,将它们捆绑为一组,并对各种UITouchGestureSubclass点进行适当的调用。 This unit tests the custom class. 此单元测试自定义类。 The behavior of the parent class I test by calling the action method registered on the UIGR subclass. 通过调用在UIGR子类上注册的action方法测试父类的行为。

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. 正如@tooluser建议你可以为UITouch对象提供“漂亮”的模拟。 Since there's no public constructor for UITouch supplying "real" UITouch instances is tricky and would rely on private API calls. 因为UITouch没有提供“真正的”UITouch实例的公共构造函数很棘手并且依赖于私有API调用。 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). 不幸的是,由于那些触摸模拟需要通过框架代码,你无法看到或控制你必须希望你的模拟实际上匹配他们加倍的UITouches的所有预期行为(包括我们看不到的任何私人行为)。 That makes me nervous. 这让我很紧张。

Alternately you can follow @Sulthan's solution and test your gesture recognizers via UI driving integration tests. 或者,您可以关注@ Sulthan的解决方案,并通过UI驱动集成测试测试您的手势识别器。 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. 使用UIAutomation或第三方测试框架(如FrankKIF)生成与视图的交互,并断言手势识别器按预期触发。 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. 基本上你写了一个javascript,然后你在Automation下的Instruments中运行它。

It's not documented very well and there are almost no tutorials. 它没有很好地记录,几乎没有教程。 Prepare to be frustrated. 准备沮丧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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