最近在读一本有年头的书—— [Test-Driven Development By Example](https://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530)。
Chapter 4 里展示了在编写测试代码的过程中,可以利用被测对象的functionality
(例子中是为对象新增的 `equals` 方法)来改进代码,取代原有测试中直接访问
被测对象的field的方式。由此可以将那个 field 改为 `private`。
不难看出,这里引入了一层“间接”性。Kent 很坦率的指出:
> Notice that we have opened ourselves up to a risk. If the test
> for equality fails to accurately check the equality is working,
> then the test for multiplication could also fail ...
既然多了一层依赖,代码就不再直接检测原有的 equality,这究竟是不是一件坏事呢?
> This is a risk that we actively manage in TDD.
> We aren't striving for perfection. By saying everyting two ways -
> both as code and as tests - we hope to reduce our defects enough to
> move forward with confidence.
所以,TDD本身不是为了盲目的追求测试覆盖率,它的本质是让开发者通过编写充分
的测试来对自己的代码获得信心!
> From time to time our reasonging will fail us and a defect will slip
> through. When that happens, we learn our lesson about the test we
> should have written and move on.
评论