Rhino Mocks 上的 mock 和 stub 有什么区别?

编程入门 行业动态 更新时间:2024-10-27 04:33:44
本文介绍了Rhino Mocks 上的 mock 和 stub 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我玩得还不够多,通常使用模拟,但我想知道这两者之间有什么区别以及何时在 Rhino Mocks 上使用其中一个.

I haven't play enough with this and usually use mocks, but I wonder what are the differences between this two and when to use one or the other on Rhino Mocks.

更新:

我还在 Ayende's 中找到了我的问题的答案单词:

存根和模拟之间的区别您可以在本文中获得这些术语的实际定义:Mocks Aren't Stubs.我想从 Rhino Mocks 的角度来关注不同之处.

The difference between stubs and mocks You can get the actual definition of the these terms in this article: Mocks Aren't Stubs. I want to focus on the difference from the point of view of Rhino Mocks.

模拟是我们可以设置期望的对象,它将验证期望的操作确实发生了.存根是您用来传递给被测代码的对象.您可以对其设置期望,因此它会以某些方式起作用,但这些期望永远不会得到验证.存根的属性会自动表现得像普通属性一样,您不能对它们设置期望.

A mock is an object that we can set expectations on, and which will verify that the expected actions have indeed occurred. A stub is an object that you use in order to pass to the code under test. You can setup expectations on it, so it would act in certain ways, but those expectations will never be verified. A stub's properties will automatically behave like normal properties, and you can't set expectations on them.

如果您想验证被测代码的行为,您将使用具有适当期望的模拟,并进行验证.如果您只想传递一个可能需要以某种方式执行但不是此测试重点的值,您将使用存根.

If you want to verify the behavior of the code under test, you will use a mock with the appropriate expectation, and verify that. If you want just to pass a value that may need to act in a certain way, but isn't the focus of this test, you will use a stub.

重要提示:存根永远不会导致测试失败.

IMPORTANT: A stub will never cause a test to fail.

推荐答案

根据this

... 简单来说,Mock 和 Stub 对象是有区别的并且 RhinoMocks 认识到允许我们编写更好的测试陈述他们的目的.

... Put simply there is a difference between Mock and Stub objects and RhinoMocks recognizes that allowing us to write tests that better state their purpose.

模拟对象用于定义期望,即:在这种情况下,我期望方法 A() 用这样那样的参数调用.模拟记录并验证此类期望.

Mock objects are used to define expectations i.e: In this scenario I expect method A() to be called with such and such parameters. Mocks record and verify such expectations.

另一方面,存根有不同的用途:它们不记录或验证期望,而是允许我们替换"行为,假"对象的状态,以便利用测试场景...

Stubs, on the other hand have a different purpose: they do not record or verify expectations, but rather allow us to "replace" the behavior, state of the "fake"object in order to utilize a test scenario ...

更多推荐

Rhino Mocks 上的 mock 和 stub 有什么区别?

本文发布于:2023-10-08 02:24:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   Mocks   Rhino   stub   mock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!