起订量要求?违背了目的?
是否需要虚拟化您想要模拟的所有属性访问器就违背了模拟的目的?
我的意思是,如果我必须修改我的对象并虚拟化我想要模拟的每个访问器,我难道不能继承我的类并自己模拟它吗?
Doesn't being required to virtualize all property accessors you want to mock kind of defeat the purpose of mocking?
I mean, if I have to modify my object and virtualize every single accesor I want to mock, couldn't I just as well inherit my class and mock it myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题非常有效,但如果你仔细想想,没有其他方法可以模拟课程。如果你采用一个接口,它只是一个契约,所以模拟框架可以模拟你想要的方式,但如果你采用一个类,它已经有它的成员的实现。
因此,模拟框架为了能够模拟类成员,必须从类继承并根据请求覆盖成员的行为,为此只有虚拟成员才能工作。
例如。如果你有(我正在展示方法,但属性也是如此)
,那么模拟框架可能会创建类似这样的东西来模拟
Now 当你有
now 当你调用时,
你不打算让它调用实际的实现,但因为它是一个非虚拟成员,它会调用
Foo
的Bar()
,另一方面,调用
会调用
MockFoo
的VirtualBar()
因为它是一个虚拟成员,它将具有模拟框架根据请求注入的行为。Your question is very valid but if you think about it,there is no other way to mock a class. If you take an interface, it's just a contract so the mock framework can mock how ever you want it but if you take a class, it already has an implementation for it's members.
So the mock framework, in order to be able to mock the class members, has to inherit from the class and override the member's behavior as requested and for this purpose only virtual members will work.
For eg. if you have (I'm showing methods but the same is true for properties)
then the mock framework probably creates something like this to mock
Now when you have
now when you call
you don't intend for it to call the actual implementation but since it's a non virtual member, it will call the
Foo
'sBar()
on the other hand, calling
would call
MockFoo
'sVirtualBar()
as it's a virtual member which would have the behavior injected by the mock framework as requested.