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

前端之家收集整理的这篇文章主要介绍了单元测试 – 什么是“存根”?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,继续我的新年决议,以获得更多的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对象“期望”某些方法调用,并且通常导致单元测试失败,如果他们的期望不符合。存根对象提供固定响应(并且可以由辅助程序库自动生成),但通常不会直接导致单元测试失败。它们通常只是被使用,以便您测试的对象获得它需要完成其工作的数据。

猜你在找的设计模式相关文章