C#中 - 如何模拟没有接口的类? [英] c# - How do I mock a class without an interface?

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

问题描述

我在.NET 4.0工作使用C#在Windows 7

I am working on .NET 4.0 using C# in Windows 7.

我想测试使用模拟一些方法之间的通信。唯一的问题是,我想这样做没有实现一个接口。那可能吗?我刚刚读了很多话题,关于mock对象的一些教程,但他们都用来模拟接口,而不是类。我试图用犀牛和最小起订量的框架。

I want to test the communication between some methods using mock. The only problem is that I want to do it without implement an interface. Is that possible? I just read a lot of topics and some tutorials about mock objects, but all of them used to mock interfaces, and not the classes. I tried to use Rhino and Moq frameworks.

推荐答案

只要选中你需要假以的任何方法的虚拟(而不是私人)。然后,你将能够创建一个假的,可以覆盖的方法。

Simply mark any method you need to fake as virtual (and not private). Then you will be able to create a fake that can override the method.

如果您使用新莫克和LT;类型> 键,你没有一个无参数的构造函数,那么你可以通过参数上面调用的参数,因为它需要一个类型参数对象

If you use new Mock<Type> and you don't have a parameterless constructor then you can pass the parameters as the arguments of The above call as it takes a type of param Objects

这篇关于C#中 - 如何模拟没有接口的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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