您应该只模拟自己拥有的类型吗? [英] Should you only mock types you own?

查看:62
本文介绍了您应该只模拟自己拥有的类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了解决方案

取决于您是指模拟还是模拟™…

鉴于您只是使用一个模拟框架(例如Mockito)来创建存根,然后创建自己不拥有的类型的存根是完全可以的,也是合理的.

但是,如果您正在使用模拟框架(例如Mockito)来创建模拟与交易;物品,那么您最好按照字面意思进行交易.传教士.就我个人而言,我对这一运动失去了了解,所以我无法告诉你马克·李约瑟姆的建议是否应被视为犹太洁食.

除了讽刺,Mark在Hibernate中关于模拟EntityManagers的著作本身听起来还很合理.但是我怀疑我们是否可以从该特定案例中推广诸如您永远不会拥有的模拟类型"之类的规则.有时这可能是有道理的,有时却没有.

I read through TDD: Only mock types you own entry by Mark Needham and would like to know if this is best practice or not?

Please note that he is not against mocking, but against mocking directly - he does say that writing a wrapper and mocking that is fine.

解决方案

Depends whether you mean mock or mock™…

Given you are just using a mock framework (as eg Mockito) to create stubs, then creating stubs of types you don't own is totally okay and reasonable.

If however, you are using a mock framework (as eg Mockito) to create mock™ objects, then you best literally follow the advice of the mock™ evangelists. Personally I lost touch to that movement, so I cannot tell you whether Mark Needham's advice is to be considered kosher or not.

Irony aside, what Mark writes about mocking of EntityManagers in Hibernate sounds reasonable by itself. But I doubt that we can generalize a rule like "never mock types you don't own" from that specific case. Sometime it might make sense, sometimes not.

这篇关于您应该只模拟自己拥有的类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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