嘲笑的对象 - 申报所有的方法为虚拟的或使用的界面? [英] Mocking objects - declare all methods as virtual or use interface?

查看:131
本文介绍了嘲笑的对象 - 申报所有的方法为虚拟的或使用的界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,不像在Java中,方法不是虚拟的默认。为了使用大多数模拟对象的框架,你要么必须标记你想的'真实'的对象对你的模拟用虚拟的方式,或者你必须有,你可以模拟接口所测试的类将接受代替执行。

In .net, unlike in Java, methods are not virtual by default. In order to use most mock object frameworks, you either have to mark the methods that you want to use on your mock as virtual on the `real' object, or you have to have an interface that you can mock that the class under test will accept in lieu of the implementation.

这似乎是不好的形式去纪念每一个方法作为虚拟的,但它也似乎是不好的形式定义一个接口,每一个类。

It seems like bad form to go and mark every method as virtual, but it also seems like bad form to define an interface for every single class.

什么是最好的事情?

推荐答案

我的经验法则是,如果我期望有多个实现,无论是实际的具体应用类或单个应用程序的实施和假冒实施单位定义接口测试。如果我只想到一个单一的实现和类并不需要伪装(最不),那么我会去的虚方法途径和重构,以在需要的接口。

My rule of thumb is to define the interface if I expect to have multiple implementations, either actual concrete application classes or a single application implementation and a fake implementation for unit testing. If I only expect a single implementation and the class doesn't need faking (most don't), then I'll go the virtual method route and refactor to an interface as needed.

这篇关于嘲笑的对象 - 申报所有的方法为虚拟的或使用的界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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