你如何模拟一个密封类? [英] How do you mock a Sealed class?

查看:16
本文介绍了你如何模拟一个密封类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模拟密封类可能会很痛苦.我目前倾向于使用 Adapter 模式 来处理这个问题,但有些东西只是保持感觉很奇怪.

Mocking sealed classes can be quite a pain. I currently favor an Adapter pattern to handle this, but something about just keeps feels weird.

那么,模拟密封类的最佳方式是什么?

Java 答案非常受欢迎.事实上,我预计 Java 社区处理这个问题的时间会更长,并且可以提供很多东西.

Java answers are more than welcome. In fact, I would anticipate that the Java community has been dealing with this longer and has a great deal to offer.

但这里有一些 .NET 意见:

But here are some of the .NET opinions:

推荐答案

我的一般经验法则是,我需要模拟的对象也应该有一个通用接口.我认为这在设计上是正确的,并且使测试更容易(如果你做 TDD,通常是你得到的).有关这方面的更多信息,请参阅 Google 测试博客 最新帖子(见第 9 点).

My general rule of thumb is that objects that I need to mock should have a common interface too. I think this is right design-wise and makes tests a lot easier (and is usually what you get if you do TDD). More about this can be read in the Google Testing Blog latest post (See point 9).

另外,在过去的 4 年里,我主要从事 Java 方面的工作,我可以说,我创建最终(密封)类的次数是一方面.这里的另一条规则是我应该始终有充分的理由来密封一个课程,而不是默认密封它.

Also, I've been working mainly in Java in the past 4 years and I can say that I can count on one hand the number of times I've created a final (sealed) class. Another rule here is I should always have a good reason to seal a class, as opposed to sealing it by default.

这篇关于你如何模拟一个密封类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆