如何在 iPhone 中对这个 hitTest 覆盖进行单元测试?
如何对这个 hitTest
覆盖进行单元测试?
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView* hitView = [super hitTest:point withEvent:event];
// Do something based on hitView's properties
}
我面临的问题是 UIEvent
没有公共构造函数,因此我无法创建 UIEvent
来在 [super hitTest 上产生不同的结果:point withEvent:event]
。
或者,我可以创建一个模拟 UIEvent
,但这意味着知道 [super hitTest:point withEvent:event]
用它做什么,而我不知道,即使我做到了,可能会改变。
另一种选择是 swizzle [super hitTest:point withEvent:event]
(使用 OCMock)但我不知道是否可以仅调整超类实现。
How can I unit test this hitTest
override?
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView* hitView = [super hitTest:point withEvent:event];
// Do something based on hitView's properties
}
The problem I'm facing is that UIEvent
does not have a public constructor, and thus I can't create an UIEvent
to produce different results on [super hitTest:point withEvent:event]
.
Alternatively I could create a mock UIEvent
, but that would imply knowing what [super hitTest:point withEvent:event]
does with it, which I don't, and even if I did, it might change.
Another option would be to swizzle [super hitTest:point withEvent:event]
(with OCMock) but I don't know if it's possible to swizzle just the superclass implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以包装对 super 的调用并使用部分模拟。
在被测试的类中,创建类似的内容:
然后在您的测试类中:
You could wrap the call to super and use a partial mock.
In the class under test, create something like:
Then in your test class: