什么是“存根”?

发布于 2024-07-12 22:33:55 字数 221 浏览 5 评论 0原文

因此,为了继续我的新年决心,更多地参与 TDD,我现在开始更多地使用 犀牛模拟

我热衷于做的一件事是确保我真正了解我正在进入的内容,所以我想检查我对到目前为止所看到的内容的理解(我认为将其作为资源放在这里会很好) )。

什么是“存根”?

So, carrying on with my new years resolution to get more in to TDD, I am now starting to work more with Rhino Mocks.

One thing I am keen to do is to make sure I really grok what I am getting into, so I wanted to check my understanding of what I have seen so far (and I thought it would be good to get it up here as a resource).

What is a "Stub"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

天暗了我发光 2024-07-19 22:33:55

Martin Fowler 就此主题撰写了一篇优秀文章。 摘自那篇文章:

Meszaros 使用术语“测试替身”作为用于代替真实对象进行测试的任何类型假装对象的通用术语。 这个名字来源于电影中特技替身的概念。 (他的目标之一是避免使用任何已经广泛使用的名称。)Meszaros 然后定义了四种特殊类型的双精度数:

  • 虚拟对象被传递但从未实际使用。 通常它们仅用于填充参数列表。
  • 假对象实际上有有效的实现,但通常会采取一些捷径,这使得它们不适合生产(内存数据库就是一个很好的例子)。
  • 存根为测试期间拨打的电话提供预设答案,通常不会对测试编程之外的任何内容做出响应。 存根还可以记录有关调用的信息,例如记住它“发送”的消息的电子邮件网关存根,或者可能只记录它“发送”的消息数量。
  • 模拟就是我们在这里讨论的内容:按照预期进行预编程的对象,这些预期形成了它们预期接收的调用的规范。

用我自己的话来说:模拟对象“期望”对它们调用某些方法,如果不满足它们的期望,通常会导致单元测试失败。 存根对象提供预设响应(并且可以由帮助程序库自动生成),但通常不会直接导致单元测试失败。 它们通常只是用来让您正在测试的对象获取完成其工作所需的数据。

Martin Fowler wrote an excellent article on this subject. From that article:

Meszaros uses the term Test Double as the generic term for any kind of pretend object used in place of a real object for testing purposes. The name comes from the notion of a Stunt Double in movies. (One of his aims was to avoid using any name that was already widely used.) Meszaros then defined four particular kinds of double:

  • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
  • Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.
  • Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

To put it in my own words: mock objects "expect" certain methods to be called on them, and typically cause a unit test to fail if their expectations aren't met. Stub objects provide canned responses (and can be autogenerated by helper libraries), but typically do not directly cause the unit test to fail. They are typically just used so that the object you're testing gets the data it needs to do its work.

药祭#氼 2024-07-19 22:33:55

“存根”是一个接口的实现,它的存在是为了提供某种类型的数据/响应。 例如:

  • 数据集
  • 用户的
  • 列表Xml 文件

通常这将由另一个服务(可以是 Web 服务、另一个应用程序、数据库)提供,但为了提高代码的可测试性,结果是“伪造的”。

这样做的一个主要好处是它允许根据预期数据在单元测试中做出断言。 如果由于数据错误而出现错误,则可以轻松添加测试、创建新的存根(复制数据错误)并生成代码来纠正错误。

存根模拟的不同之处在于,它们用于表示和测试对象的状态,而模拟则测试其交互< /强>。

A "stub" is an implementation of an interface that exists to provide data/a response of some sort. For example:

  • a DataSet
  • list of Users
  • an Xml File

Normally this would be provided by another service (be it Web Service, another application, a database) but in order to improve the testability of the code, the results are "faked".

A major benefit of this is that it allows assertions to be made in unit tests based on the data expected. If errors arise due to data errors, then tests can easily be added, a new stub created (replicating the data error) and code produced to correct the error.

Stubs differ to Mocks in that they are used to represent and test the state of an object, whereas a Mock tests its interaction.

滿滿的愛 2024-07-19 22:33:55

我最近遇到了这个问题,并认识到 StubDriver 之间的比较非常清晰且有用:

基本上,存根和驱动程序是除了声明之外实际上不执行任何操作的例程它们本身以及它们接受的参数。 然后,代码的其余部分可以获取这些参数并将它们用作输入。

+---------+-------------------------------+-------------------------------+
|         | Stub                          | Driver                        |
+---------+-------------------------------+-------------------------------+
| Type    | Dummy codes                   | Dummy codes                   |
+---------+-------------------------------+-------------------------------+
| Used in | Top Down Integration          | Bottom Up Integration         |
+---------+-------------------------------+-------------------------------+
| Purpose | To allow testing of the upper | To allow testing of the lower |
|         | levels of the code, when the  | levels of the code, when the  |
|         | lower levels of the code are  | upper levels of the code are  |
|         | not yet developed.            | not yet developed.            |
+---------+-------------------------------+-------------------------------+
| Example | A and B are components.       | A and B are components.       |
|         | A ---> B                      | A ---> B                      |
|         |                               |                               |
|         | A has been developed.         | A still needs to be developed.|
|         | B still needs to be developed.| B has been developed.         |
|         | Therefore, stub is used       | Therefore, driver is used     |
|         | in place of B to imitate it.  | in place of A to imitate it   |
|         |                               |                               |
|         | A ---> Stub                   | Driver ---> B                 |
+---------+-------------------------------+-------------------------------+

来自存根和驱动程序之间的区别

I faced the question recently and recognised that this comparison between Stub and Driver is really clear and helpful:

Basically, stubs and drivers are routines that don’t actually do anything except declare themselves and the parameters they accept. The rest of the code can then take these parameters and use them as inputs.

+---------+-------------------------------+-------------------------------+
|         | Stub                          | Driver                        |
+---------+-------------------------------+-------------------------------+
| Type    | Dummy codes                   | Dummy codes                   |
+---------+-------------------------------+-------------------------------+
| Used in | Top Down Integration          | Bottom Up Integration         |
+---------+-------------------------------+-------------------------------+
| Purpose | To allow testing of the upper | To allow testing of the lower |
|         | levels of the code, when the  | levels of the code, when the  |
|         | lower levels of the code are  | upper levels of the code are  |
|         | not yet developed.            | not yet developed.            |
+---------+-------------------------------+-------------------------------+
| Example | A and B are components.       | A and B are components.       |
|         | A ---> B                      | A ---> B                      |
|         |                               |                               |
|         | A has been developed.         | A still needs to be developed.|
|         | B still needs to be developed.| B has been developed.         |
|         | Therefore, stub is used       | Therefore, driver is used     |
|         | in place of B to imitate it.  | in place of A to imitate it   |
|         |                               |                               |
|         | A ---> Stub                   | Driver ---> B                 |
+---------+-------------------------------+-------------------------------+

From Difference between Stub and Driver

爱她像谁 2024-07-19 22:33:55

我相信“存根”来自 STARTUpBlock。 它用于指代自动生成的代码部分,以帮助您(开发人员)开始使用。

I believe "stub" comes from STartUpBlock. it is used to refer to parts of code that are auto generated to help you, the developer, get started.

伤感在游骋 2024-07-19 22:33:55

“存根”或“存根方法”被设计为起始代码或尚未开发的代码的临时替代品。 它是由 IDE 生成的内置代码。 存根方法实际上是用于测试特定类的方法的方法。 在实际开发过程中,通过输入一些局部变量的值,并检查输出是否正确来使用。 查找代码中的错误非常重要。

A "stub" or "stub method" is designed to be a starter-code or a temporary substitute for yet-to-be-developed code. It's a built-in code generated by an IDE. Stub methods are actually methods used for testing methods of a particular class. It is used by inputting some values for the local variables in your actual development methods and check if the output is correct. It is important in finding bugs in your code.

花开半夏魅人心 2024-07-19 22:33:55

经过一些研究并基于我在编码生涯中遇到的存根文件,我会说存根文件只是一个包含文件的全部或部分实现的文件。 它可以帮助开发人员开始编码。

After some research and based on stub files that I faced during my coder life, I would say that a stub file is just a file that contains a entire or part of the implementation of a file. It helps developers to start coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文