通过测试驱动开发流程,先写测试再实现功能或修复缺陷。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "test-driven-development" 技能: 1. 下载 https://raw.githubusercontent.com/obra/superpowers/main/skills/test-driven-development/SKILL.md 2. 保存为 ~/.claude/skills/test-driven-development/SKILL.md 3. 装好后重载技能,告诉我可以用了
请用测试驱动开发方式实现一个获取用户资料的 API。先列出测试用例,再编写失败的单元测试,最后给出最小可行实现代码与重构建议。技术栈使用 Node.js 和 Jest。
先给出测试清单与失败测试代码,再提供通过测试的最小实现和后续重构建议。
我有一个日期格式化函数在时区边界会出错。请按 TDD 流程先写一个能复现问题的测试,再给出修复代码,并说明为什么这个修复能通过测试。
包含复现缺陷的测试、针对性的修复代码,以及测试通过原因的简要说明。
请把“订单满减和会员折扣同时生效”的规则用 TDD 方式实现。先把业务规则拆成多个测试场景,再逐步写出测试、实现和必要的重构。
输出分层测试场景、逐步实现代码,以及可读性更高的重构版本。
Write the test first. Watch it fail. Write minimal code to pass.
Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.
Violating the letter of the rules is violating the spirit of the rules.
Always:
Exceptions (ask your human partner):
Thinking "skip TDD just this once"? Stop. That's rationalization.
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
Write code before the test? Delete it. Start over.
No exceptions:
Implement fresh from tests. Period.
<Good> ```typescript test('retries failed operations 3 times', async () => { let attempts = 0; const operation = () => { attempts++; if (attempts < 3) throw new Error('fail'); return 'success'; }; <Good> ```typescript async function retryOperation<T>(fn: () => Promise<T>): Promise<T> { for (let i = 0; i < 3; i++) { try { return await fn(); } catch (e) { if (i === 2) throw e; } } throw new Error('unreachable'); } ``` Just enough to pass </Good> <Bad> ```typescript async function retryOperation<T>( fn: () => Promise<T>, options?: { maxRetries?: number; backoff?: 'linear' | 'exponential'; onRetry?: (attempt: number) => void; } ): Promise<T> { // YAGNI } ``` Over-engineered </Bad>digraph tdd_cycle {
rankdir=LR;
red [label="RED\nWrite failing test", shape=box, style=filled, fillcolor="#ffcccc"];
verify_red [label="Verify fails\ncorrectly", shape=diamond];
green [label="GREEN\nMinimal code", shape=box, style=filled, fillcolor="#ccffcc"];
verify_green [label="Verify passes\nAll green", shape=diamond];
refactor [label="REFACTOR\nClean up", shape=box, style=filled, fillcolor="#ccccff"];
next [label="Next", shape=ellipse];
red -> verify_red;
verify_red -> green [label="yes"];
verify_red -> red [label="wrong\nfailure"];
green -> verify_green;
verify_green -> refactor [label="yes"];
verify_green -> green [label="no"];
refactor -> verify_green [label="stay\ngreen"];
verify_green -> next;
next -> red;
}
Write one minimal test showing what should happen.
const result = await retryOperation(operation);
expect(result).toBe('success'); expect(attempts).toBe(3); });
Clear name, tests real behavior, one thing
</Good>
<Bad>
```typescript
test('retry works', async () => {
const mock = jest.fn()
.mockRejectedValueOnce(new Error())
.mockRejectedValueOnce(new Error())
.mockResolvedValueOnce('success');
await retryOperation(mock);
expect(mock).toHaveBeenCalledTimes(3);
});
Vague name, tests mock not code </Bad>
Requirements:
MANDATORY. Never skip.
npm test path/to/test.test.ts
Confirm:
Test passes? You're testing existing behavior. Fix test.
Test errors? Fix error, re-run until it fails correctly.
Write simplest code to pass the test.
Don't add features, refactor other code, or "improve" beyond the test.
MANDATORY.
npm test path/to/test.test.ts
Confirm:
Test fails? Fix code, not test.
Other tests fail? Fix now.
After green only:
Keep tests green. Don't add behavior.
Next failing test for next feature.
…
在宣称任务完成或修复成功前,先执行验证并用结果佐证结论。
用测试驱动开发流程编写新功能、修复缺陷并安全重构代码。