模拟类和接口之间有什么区别?
Rhino Mocks 中的模拟类和接口有什么区别?
What's the difference between mocking classes and interfaces in Rhino Mocks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Rhino Mocks 中的模拟类和接口有什么区别?
What's the difference between mocking classes and interfaces in Rhino Mocks?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您不能模拟具体类本身,除非该类是抽象的或具有虚拟方法。您可以伪造抽象类的契约,或者使用 Rhino Mocks 用伪造的方法覆盖虚拟方法。
模拟接口仅仅意味着您正在定义所述接口的假实现。
You can't mock concrete classes per se, unless the class is abstract or has virtual methods. You can fake the contract of the abstract class, or override virtual methods with a faked method using Rhino Mocks.
Mocking an interface simply means you are defining a fake implementation of said interface.
要模拟类,属性和函数需要声明为虚拟的。当模拟接口时,您可以毫无问题地自由模拟。
此外,如果您模拟一个类,则可以运行未模拟的代码,如果您正在模拟一个接口,则无法执行此操作。假设您想要检查当特定函数返回某个值时您的类如何运行,您可能必须模拟一个类。我建议尽可能保留模拟接口。这要容易得多。
To mock a class the properties and functions need to be declared as virtual. When mocking an interface your can mock freely without problem.
Further it is possible to run code that is not mocked if you mock a class, this can't be done if you are mocking an interface. Let's say that you want to check how your class is functioning when a specific function returns a certain value you might have to mock a class. I'd recommend staying with mocking interfaces as far as possible though. It is much much easier.