Skip to content

Red-Green-Refactor

一句话解释

TDD cycle: Red (failing 测试), Green (passing 测试), Refactor (improve code 虽然测试通过).

用大白话说

Red-Green-Refactor 是 rhythm 的 TDD. 它's three-步骤 cycle 你 repeat for 每个 feature:

  1. Red: 编写测试 for 新 behavior. 运行它. 它失败 (red) 因为 code doesn't exist or doesn't 做正确 thing yet. 这个确认测试是 actually 测试某事.
  1. Green: 编写最小值 code 需要到 制作测试通过. Don't 添加 extra features or 复杂度 — 只是制作它 通过. 运行测试. 它通过 (green).
  1. Refactor: 现在那个测试通过, improve code. Rename 变量, extract 函数, 移除 duplication. 运行测试之后每个更改. 如果某事 breaks, 你 know immediately.

The cycle ensures 你总是有 工作 code and growing 测试 suite. 'red' 步骤是 关键 — 如果新 测试通过 immediately, 它可能不 是测试任何事新.

示例

javascript
// RED — write a failing test first
test('multiply returns product', () => {
  expect(multiply(3, 4)).toBe(12);  // fails: multiply not defined
});

// GREEN — write minimum code to pass
function multiply(a, b) {
  return a * b;
}
// Run test → passes ✓

// REFACTOR — improve while tests stay green
const multiply = (a, b) => a * b;
// Run test → still passes ✓

TDD 三步循环:Red(写一个失败的测试)、Green(写最少的代码让测试通过)、Refactor(在测试保护下改进代码)。

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表