微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

所以,继续我的新年决议,以获得更多的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对象“期望”某些方法调用,并且通常导致单元测试失败,如果他们的期望不符合。存根对象提供固定响应(并且可以由辅助程序库自动生成),但通常不会直接导致单元测试失败。它们通常只是被使用,以便您测试的对象获得它需要完成其工作的数据。

原文地址:https://www.jb51.cc/javaschema/282899.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐