什么是“存根"? [英] What is a "Stub"?

查看:84
本文介绍了什么是“存根"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,继续我的新年决心以更多地参与 TDD,我现在开始更多地使用 Rhino Mocks.

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 in to, 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).

推荐答案

Martin Fowler 撰写了一篇出色的文章 关于这个问题.来自那篇文章:

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

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

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:

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

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

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.

这篇关于什么是“存根"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆