如何在 iPhone 中对这个 hitTest 覆盖进行单元测试?

发布于 2024-12-21 16:36:57 字数 663 浏览 3 评论 0原文

如何对这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2024-12-28 16:36:57

您可以包装对 super 的调用并使用部分模拟。

在被测试的类中,创建类似的内容:

-(UIView *)checkHit:(CGPoint)point withEvent:(UIEvent *)event {
    return [super hitTest:point withEvent:event];
}

然后在您的测试类中:

CGPoint testPoint = CGPointMake(1,2);
id mockHitView = [OCMockObject mockForClass:[UIView class]];
id mockSomething = [OCMockObject partialMockForObject:realObject];
[[[mockSomething stub] andReturn:mockHitView] checkHit:testPoint withEvent:[OCMArg any]];

[realObject hitTest:testPoint withEvent:nil];

You could wrap the call to super and use a partial mock.

In the class under test, create something like:

-(UIView *)checkHit:(CGPoint)point withEvent:(UIEvent *)event {
    return [super hitTest:point withEvent:event];
}

Then in your test class:

CGPoint testPoint = CGPointMake(1,2);
id mockHitView = [OCMockObject mockForClass:[UIView class]];
id mockSomething = [OCMockObject partialMockForObject:realObject];
[[[mockSomething stub] andReturn:mockHitView] checkHit:testPoint withEvent:[OCMArg any]];

[realObject hitTest:testPoint withEvent:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文