单元测试 – 什么是“存根”?

所以,继续我的新年决议,以获得更多的TDD,我现在开始工作更多与 Rhino Mocks

我渴望做的一件事是确保我真的理解我正在得到的东西,所以我想检查我对我目前为止所看到的东西的理解(我认为这将是很好的把它在这里作为一个资源)。

什么是“存根”?

Martin Fowler在这个问题上写了 an excellent 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.

用我自己的话说:mock对象“期望”某些方法调用,并且通常导致单元测试失败,如果他们的期望不符合。存根对象提供固定响应(并且可以由辅助程序库自动生成),但通常不会直接导致单元测试失败。它们通常只是被使用,以便您测试的对象获得它需要完成其工作的数据。

相关文章

适配器模式将一个类的接口转换成客户期望的另一个接口,使得原本接口不兼容的类可以相互合作。
策略模式定义了一系列算法族,并封装在类中,它们之间可以互相替换,此模式让算法的变化独立于使用算法...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,它是针对软件开发中经常遇到的一些设计问题...
模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中,使得子类可以在不改变算法结...
迭代器模式提供了一种方法,用于遍历集合对象中的元素,而又不暴露其内部的细节。
外观模式又叫门面模式,它提供了一个统一的(高层)接口,用来访问子系统中的一群接口,使得子系统更容...