我什么时候应该通过手动创建“存根”来存根类型?版本,而不是使用模拟框架
在某些情况下,是否有利于手动创建存根类型,而不是在测试时使用模拟框架(例如 Rhino Mocks)。
我们在项目中采用了这两种方法。当我查看对象的存根版本的长列表时,我的直觉是它会增加维护开销,并将存根的实现移离测试点。
Are there any circumstances where it is favourable to manually create a stub type, as opposed to using a mocking framework (such as Rhino Mocks) at the point of test.
We take both these approaches in our projects. My gut feel when I look at the long list of stub versions of objects is that it will add maintenance overhead, and moves the implementation of the stub away from the point of test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您达到存根占用过多维护时间的程度时,就该转向存根/模拟框架了。所以我想你自己已经给出了答案。
目前,为了简单起见,我仍在当前项目中使用存根。我只对几种类型使用存根,并且通常只有一个存根,有时使用两个存根来重新创建错误,但仅此而已。
When you reach the point where stubs take up too much maintenance time, it's time to move on to a stub/mock framework. So I think you have given the answer already yourself.
Currently I'm still using stubs with my current project, for the sake of simplicity. There are only a few types for which I use a stub, and usually there's only one stub, sometimes two to recreate errors, but no more.
出于自动化单元测试的目的,模拟几乎总是更好的选择。因为它们使用反射来模拟对象,所以当您进行更改时它们将保持最新状态。
我创建存根的唯一一次是为了集成测试 - 例如,您可以存根 SMS 发送服务,以便您可以运行一段时间的集成/用户测试,而无需为发送 SMS 消息付费(相反,消息被存储,以便它们可以检查)。
For the purposes of automated unit tests, mocks are almost always the better option. Because they use reflection to mock an object, they will stay up-to-date when you make changes.
The only time I create a stub is for integration testing - for example, you might stub an SMS Sending Service so you can run a period of integration / user testing without being charged for sending out SMS Messages (instead, the messages are stored so they can be checked).