Stubs和Mocks区别 (Stubs vs. Mocks)

来源:互联网 发布:淘宝卖家怎么注销店铺 编辑:程序博客网 时间:2024/06/10 08:58

翻译自《Programming Groovy - Dynamic Productivity for the jdk Developer》的P243。

 

原文如下:

Stubs vs. Mocks

In the article “Mocks Aren’t Stubs,” (http://martinfowler.com/articles/mocksArentStubs.html), Martin Fowler discusses the difference between stubs and mocks. A stub stands in for a real object. It simply reciprocates the coached expected response when called by the code being tested. The response is set up to satisfy the needs for the test to pass. A mock object does a lot more than a stub. It helps you ensure your code is interacting with its dependencies, the collaborators, as expected. It can keep track of the sequence and number of calls your code makes on the collaborator it stands in for. It ensures proper parameters are passed in to method calls. While stubs verify state, mocks verify behavior. When you use a mock in your test, it verifies not only the state but also the behavior of the interaction of your code with its dependencies. Groovy provides support for creating both stubs and mocks, as you will see in Section 16.10, Mocking Using the Groovy Mock Library, on page 254.

 

 

翻译如下:

Stubs和Mocks的区别

在文章“Mocks不是Stubs”中(http://martinfowler.com/articles/mocksArentStubs.html),马丁·福勒讨论了stubs和mocks之间的区别。

 

stub代表的是一个真实的对象。在测试代码中被调用时,它简单的按照之前对它设定来应答调用者(对stub应答的设定过程称这为训练coach)。对stub应答的设定是为了通过测试。

 

mock对象要比stub做的事情多得多:

  • 帮你确定你的代码和它的依赖(称为合作者collaborator)有你期望的交互。
  • 跟踪你的在Mock对象代表的合作者执行调用的序列和次数。
  • 保证方法调用传递了合适的参数。

stubs只检查的是状态(state),而mocks检查了行为(behavior)。在测试中使用mock,不仅检查你测试和其依赖之间和状态,而且还有行为。

 

 

PS:

顺便推荐一下《Programming Groovy - Dynamic Productivity for the jdk Developer》这本书。

这是一本Groovy进阶的书,入门可以先看一下《Groovy Recipes -Greasing the Wheels of jdk》或是《Groovy Programming -An Introduction for jdk Developers》。Groovy的资料可以看看:http://blog.csdn.net/oldrat/archive/2010/02/22/5317366.aspx

 

《Programming Groovy》中有关的元编程的讲解应该是最全面的。

更好的是,对动态语言的编程的最佳实践也讲了很多。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 pr字幕打不了字怎么办 pr手写字多笔画怎么办 家中挖矿噪音很大怎么办 学了栏目包装不想干怎么办 在文本打字乱了怎么办 体考后觉得成绩有出入怎么办 荧光棒进眼睛里怎么办 无法读源文件或磁盘怎么办 爱剪辑视频横着怎么办 pr界面字体太小怎么办 0p手机卡槽坏了怎么办 pr视频有水波纹怎么办 手机耳机插口插上没声音怎么办 xp系统电脑没有声音怎么办 win10笔记本耳机没声音怎么办 电脑插耳机还是外放怎么办 主机电脑耳机插孔坏了怎么办 苹果6plus有点卡怎么办 手机拨通电话后黑屏怎么办 手机拨通后没声音怎么办 苹果手机铃声不响了怎么办 婴儿喘气粗好像有痰怎么办 金毛呼吸声很大怎么办 回看福建导视该怎么办 楼上木地板走路声音都很响怎么办 班里团支不搭理我我该怎么办 客户买家具说贵怎么办 英雄联盟超出显示范围怎么办 脸上有斑点怎么办内条 脖子扭到了一动就疼怎么办 杭州限行在路上怎么办 在路上限行了怎么办 微信号不能进群怎么办 在微信约妹子被骗2000怎么办 qq号进不了热聊怎么办 网上买彩票中五百万怎么办 别人登我支付宝怎么办 医保刷卡刷到现金支付怎么办 微信诈骗立案后怎么办 奔跑吧诈骗填写了身份证怎么办 快手绑定的手机号丢了怎么办