Moq中的单元测试模拟/存根定义 [英] Unit Testing Mock/Stub definitions in Moq
问题描述
关于单元测试的任何读物或建议都总是建议在模拟和存根的定义之间存在明显的区别.我目前对这些定义的理解如下
Any reading or advice I've been given on Unit Testing has always suggested a distinct difference between the definition of a Mock and a Stub. My current understanding of these definitions are as follows
模拟:将用于 您进行测试以做出最终断言
Mock: A fake which will be used in your test to make a final assertion
存根:伪造品,将用于 您的测试以隔离依赖项,但 不能断言
Stub: A fake which will be used in your test to isolate a dependency but not be asserted
但是,Moq似乎仅允许创建Mocks.框架中的Stub命名空间似乎已被弃用,并建议使用Mock.SetupXXX.
However, Moq appears to only allow the creation of Mocks. The Stub namespace in the framework appears to be depreciated with recommendations to use Mock.SetupXXX.
我对此有所了解吗?还是有一种普遍的理解,那就是模拟对象实际上可以用作存根?
Am I missing something in my understanding of this? Or is there a general understanding that a mock object can infact be used as nothing more that a stub?
也许我是个书呆子,只是我一直发现编程语言非常严格,并且喜欢正确使用它,尤其是在其他开发人员可能接管一个项目的时候.
Perhaps I am being pedantic, it's just that I have always found language in programming to be very strict and prefer to get my usage of it correct, especially when other developers might be taking over a project.
推荐答案
根据 Moq项目站点,起订量提供:
通过简单的 MockBehavior 枚举对模拟行为进行粒度控制(无需了解模拟,存根,假冒,动态模拟等之间的理论区别是什么)
Granular control over mock behavior with a simple MockBehavior enumeration (no need to learn what's the theoretical difference between a mock, a stub, a fake, a dynamic mock, etc.)
在模拟,存根之间缺乏区分,这是一个故意的设计决策;我最喜欢的一项设计决策.如果需要 true 模拟,则可以在其上调用Verify()
.如果不是,则没有Verify()
.我喜欢简单性,但我没有发现自己错过了mock
和stub
之间的区别.
The lack of distinction between mocks, stubs, and such is a deliberate design decision; A design decision which I, for one, prefer. If I need a true mock, I call Verify()
on it. If not, there's no Verify()
. I like the simplicity, and I haven't found myself missing the distinction between mock
and stub
.
这篇关于Moq中的单元测试模拟/存根定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!