有效地实践 TDD 不仅仅是了解 Red-Green-Refactor 循环——它涉及采取小步骤、编写正确的测试、正确地执行重构步骤,以及判断何时 TDD 适用。做得好的话,它会推动好的设计和高质量、经过充分测试的代码。
严格的循环
1. RED — write ONE small failing test for the NEXT bit of behavior (run it, see it fail —
confirms the test works and the feature is missing)
2. GREEN — write the SIMPLEST code to pass (don't over-engineer; even "fake it" first)
3. REFACTOR — now improve the design (remove duplication, clean up) with tests as a safety net
→ SMALL steps; one behavior at a time; stay in short cycles.
