tdd-workflows-tdd-green

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Green Phase: Simple function

绿灯阶段:简单函数

def product_list(request): products = Product.objects.all() return JsonResponse({'products': list(products.values())})
def product_list(request): products = Product.objects.all() return JsonResponse({'products': list(products.values())})

Refactor: Class-based view

重构:基于类的视图

class ProductListView(View): def get(self, request): products = Product.objects.all() return JsonResponse({'products': list(products.values())})
class ProductListView(View): def get(self, request): products = Product.objects.all() return JsonResponse({'products': list(products.values())})

Refactor: Generic view

重构:通用视图

class ProductListView(ListView): model = Product context_object_name = 'products'
undefined
class ProductListView(ListView): model = Product context_object_name = 'products'
undefined

Express Patterns

Express 模式

Inline → Middleware → Service Layer:
javascript
// Green Phase: Inline logic
app.post('/api/users', (req, res) => {
  const user = { id: Date.now(), ...req.body };
  users.push(user);
  res.json(user);
});

// Refactor: Extract middleware
app.post('/api/users', validateUser, (req, res) => {
  const user = userService.create(req.body);
  res.json(user);
});

// Refactor: Full layering
app.post('/api/users',
  validateUser,
  asyncHandler(userController.create)
);
内联 → 中间件 → 服务层:
javascript
// 绿灯阶段:内联逻辑
app.post('/api/users', (req, res) => {
  const user = { id: Date.now(), ...req.body };
  users.push(user);
  res.json(user);
});

// 重构:提取中间件
app.post('/api/users', validateUser, (req, res) => {
  const user = userService.create(req.body);
  res.json(user);
});

// 重构:完整分层
app.post('/api/users',
  validateUser,
  asyncHandler(userController.create)
);

Use this skill when

适用场景

  • Moving from red to green in a TDD cycle
  • Implementing minimal behavior to satisfy tests
  • You want to keep implementation intentionally simple
  • 在TDD循环中从红灯阶段过渡到绿灯阶段时
  • 实现满足测试要求的最简行为时
  • 希望刻意保持实现简洁时

Do not use this skill when

不适用场景

  • You are refactoring for design or performance
  • Tests are already passing and you need new requirements
  • You need a full architectural redesign
  • 为优化设计或性能进行重构时
  • 测试已通过且需要实现新需求时
  • 需要进行完整架构重设计时

Instructions

操作步骤

  1. Review failing tests and identify the smallest fix.
  2. Implement the minimal change to pass the next test.
  3. Run tests after each change to confirm progress.
  4. Record shortcuts or debt for the refactor phase.
  1. 查看失败的测试,确定最小修复方案。
  2. 实施最小改动以通过下一个测试。
  3. 每次改动后运行测试,确认进展。
  4. 记录重构阶段需要处理的捷径或技术债务。

Safety

注意事项

  • Avoid bypassing tests to make them pass.
  • Keep changes scoped to the failing behavior only.
  • 避免通过绕过测试的方式让测试通过。
  • 仅针对失败的行为进行改动,控制改动范围。

Resources

参考资源

  • resources/implementation-playbook.md
    for detailed patterns and examples.
  • resources/implementation-playbook.md
    :包含详细模式和示例的实现手册。