模拟是否破坏了“测试接口,而不是实现"?口头禅? [英] Do mocks break the "test the interface, not the implementation" mantra?

查看:62
本文介绍了模拟是否破坏了“测试接口,而不是实现"?口头禅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Wikipedia(重点是我的,已删除内部参考文献):

在单元测试的艺术"一书中,模拟被描述为伪造的对象,通过验证与对象的交互是否发生来帮助确定测试是失败还是通过.

在我看来,模拟正在测试实现.具体来说,他们测试实现与特定对象交互的方式.

我正确解释了吗?模拟是否故意破坏了测试接口,而不是实现"的口头禅?或者,模拟测试不是单元测试吗?

解决方案

正确的模拟并不遵循测试接口而不是实现"的经典口号.模拟不是使用状态验证,而是使用行为验证.

来自 http://martinfowler.com/articles/mocksArentStubs.html :

假人使用行为验证.

因此,模拟测试与方法的实现更加相关.更改对协作者的呼叫的性质通常会导致模拟测试失败.

这种耦合导致一些问题.最重要的是对测试驱动开发的影响.使用模拟测试,编写测试可以使您考虑行为的实现-实际上,模拟测试人员将其视为一种优势.但是,古典主义者认为,仅考虑外部接口会发生什么,并把所有实现的考虑都留到完成编写测试之后,这一点很重要.

From Wikipedia (emphasis mine, internal references removed):

In the book "The Art of Unit Testing" mocks are described as a fake object that helps decide whether a test failed or passed by verifying whether an interaction with an object occurred.

It seems to me that mocks are testing implementation. Specifically, they test that the way that the implementation interacted with a particular object.

Am I interpreting this correctly? Are mocks an intentional breaking of the "test the interface, not the implementation" mantra? Or, are mocks testing at a level other than unit tests?

解决方案

Correct, mocks do not follow the classicist mantra of "test the interface, not the implementation". Instead of state verification, mocks use behavior verification.

From http://martinfowler.com/articles/mocksArentStubs.html:

Mocks use behavior verification.

Mockist tests are thus more coupled to the implementation of a method. Changing the nature of calls to collaborators usually cause a mockist test to break.

This coupling leads to a couple of concerns. The most important one is the effect on Test Driven Development. With mockist testing, writing the test makes you think about the implementation of the behavior - indeed mockist testers see this as an advantage. Classicists, however, think that it's important to only think about what happens from the external interface and to leave all consideration of implementation until after you're done writing the test.

这篇关于模拟是否破坏了“测试接口,而不是实现"?口头禅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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