testing-anti-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTesting Anti-Patterns
测试反模式
Overview
概述
Tests must verify real behavior, not mock behavior. Mocks are a means to isolate, not the thing being tested.
Core principle: Test what the code does, not what the mocks do.
Following strict TDD prevents these anti-patterns.
测试必须验证真实行为,而非Mock行为。Mock是用于实现隔离的手段,而非测试的目标。
核心原则: 测试代码的实际功能,而非Mock的表现。
遵循严格的TDD规范可以避免这些反模式。
The Iron Laws
铁律
1. NEVER test mock behavior
2. NEVER add test-only methods to production classes
3. NEVER mock without understanding dependencies1. NEVER test mock behavior
2. NEVER add test-only methods to production classes
3. NEVER mock without understanding dependenciesAnti-Pattern 1: Testing Mock Behavior
反模式1:测试Mock行为
The violation:
typescript
// ❌ BAD: Testing that the mock exists
test('renders sidebar', () => {
render(<Page />);
expect(screen.getByTestId('sidebar-mock')).toBeInTheDocument();
});Why this is wrong:
- You're verifying the mock works, not that the component works
- Test passes when mock is present, fails when it's not
- Tells you nothing about real behavior
your human partner's correction: "Are we testing the behavior of a mock?"
The fix:
typescript
// ✅ GOOD: Test real component or don't mock it
test('renders sidebar', () => {
render(<Page />); // Don't mock sidebar
expect(screen.getByRole('navigation')).toBeInTheDocument();
});
// OR if sidebar must be mocked for isolation:
// Don't assert on the mock - test Page's behavior with sidebar present错误案例:
typescript
// ❌ BAD: Testing that the mock exists
test('renders sidebar', () => {
render(<Page />);
expect(screen.getByTestId('sidebar-mock')).toBeInTheDocument();
});错误原因:
- 你在验证Mock是否正常工作,而非组件是否正常工作
- 只要Mock存在测试就会通过,Mock不存在就失败
- 完全无法验证真实的业务表现
你的搭档会这么纠正你: "我们难道是在测试Mock的行为吗?"
修复方案:
typescript
// ✅ GOOD: Test real component or don't mock it
test('renders sidebar', () => {
render(<Page />); // Don't mock sidebar
expect(screen.getByRole('navigation')).toBeInTheDocument();
});
// OR if sidebar must be mocked for isolation:
// Don't assert on the mock - test Page's behavior with sidebar presentGate Function
准入检查
BEFORE asserting on any mock element:
Ask: "Am I testing real component behavior or just mock existence?"
IF testing mock existence:
STOP - Delete the assertion or unmock the component
Test real behavior instead对任何Mock元素做断言前:
自问:"我是在测试真实组件的行为,还是只是在验证Mock是否存在?"
如果是在测试Mock是否存在:
立刻停止 - 删除该断言,或者取消对组件的Mock
转而测试真实的业务行为Anti-Pattern 2: Test-Only Methods in Production
反模式2:生产代码中存在仅测试用方法
The violation:
typescript
// ❌ BAD: destroy() only used in tests
class Session {
async destroy() { // Looks like production API!
await this._workspaceManager?.destroyWorkspace(this.id);
// ... cleanup
}
}
// In tests
afterEach(() => session.destroy());Why this is wrong:
- Production class polluted with test-only code
- Dangerous if accidentally called in production
- Violates YAGNI and separation of concerns
- Confuses object lifecycle with entity lifecycle
The fix:
typescript
// ✅ GOOD: Test utilities handle test cleanup
// Session has no destroy() - it's stateless in production
// In test-utils/
export async function cleanupSession(session: Session) {
const workspace = session.getWorkspaceInfo();
if (workspace) {
await workspaceManager.destroyWorkspace(workspace.id);
}
}
// In tests
afterEach(() => cleanupSession(session));错误案例:
typescript
// ❌ BAD: destroy() only used in tests
class Session {
async destroy() { // Looks like production API!
await this._workspaceManager?.destroyWorkspace(this.id);
// ... cleanup
}
}
// In tests
afterEach(() => session.destroy());错误原因:
- 生产类被仅测试用途的代码污染
- 如果在生产环境被意外调用会造成严重风险
- 违反YAGNI原则和关注点分离原则
- 混淆了对象生命周期和实体生命周期
修复方案:
typescript
// ✅ GOOD: Test utilities handle test cleanup
// Session has no destroy() - it's stateless in production
// In test-utils/
export async function cleanupSession(session: Session) {
const workspace = session.getWorkspaceInfo();
if (workspace) {
await workspaceManager.destroyWorkspace(workspace.id);
}
}
// In tests
afterEach(() => cleanupSession(session));Gate Function
准入检查
BEFORE adding any method to production class:
Ask: "Is this only used by tests?"
IF yes:
STOP - Don't add it
Put it in test utilities instead
Ask: "Does this class own this resource's lifecycle?"
IF no:
STOP - Wrong class for this method给生产类新增任何方法前:
自问:"这个方法是不是只在测试中使用?"
如果是:
立刻停止 - 不要添加这个方法
转而把它放到测试工具函数中
自问:"这个类是否拥有该资源的生命周期管理权?"
如果否:
立刻停止 - 这个方法不属于当前类Anti-Pattern 3: Mocking Without Understanding
反模式3:不理解依赖就随意Mock
The violation:
typescript
// ❌ BAD: Mock breaks test logic
test('detects duplicate server', () => {
// Mock prevents config write that test depends on!
vi.mock('ToolCatalog', () => ({
discoverAndCacheTools: vi.fn().mockResolvedValue(undefined)
}));
await addServer(config);
await addServer(config); // Should throw - but won't!
});Why this is wrong:
- Mocked method had side effect test depended on (writing config)
- Over-mocking to "be safe" breaks actual behavior
- Test passes for wrong reason or fails mysteriously
The fix:
typescript
// ✅ GOOD: Mock at correct level
test('detects duplicate server', () => {
// Mock the slow part, preserve behavior test needs
vi.mock('MCPServerManager'); // Just mock slow server startup
await addServer(config); // Config written
await addServer(config); // Duplicate detected ✓
});错误案例:
typescript
// ❌ BAD: Mock breaks test logic
test('detects duplicate server', () => {
// Mock prevents config write that test depends on!
vi.mock('ToolCatalog', () => ({
discoverAndCacheTools: vi.fn().mockResolvedValue(undefined)
}));
await addServer(config);
await addServer(config); // Should throw - but won't!
});错误原因:
- 被Mock的方法存在测试依赖的副作用(写入配置)
- 为了"安全"过度Mock反而破坏了实际的业务逻辑
- 测试会因为错误的原因通过,或者出现莫名其妙的失败
修复方案:
typescript
// ✅ GOOD: Mock at correct level
test('detects duplicate server', () => {
// Mock the slow part, preserve behavior test needs
vi.mock('MCPServerManager'); // Just mock slow server startup
await addServer(config); // Config written
await addServer(config); // Duplicate detected ✓
});Gate Function
准入检查
BEFORE mocking any method:
STOP - Don't mock yet
1. Ask: "What side effects does the real method have?"
2. Ask: "Does this test depend on any of those side effects?"
3. Ask: "Do I fully understand what this test needs?"
IF depends on side effects:
Mock at lower level (the actual slow/external operation)
OR use test doubles that preserve necessary behavior
NOT the high-level method the test depends on
IF unsure what test depends on:
Run test with real implementation FIRST
Observe what actually needs to happen
THEN add minimal mocking at the right level
Red flags:
- "I'll mock this to be safe"
- "This might be slow, better mock it"
- Mocking without understanding the dependency chainMock任何方法前:
先停下 - 不要立刻Mock
1. 自问:"真实方法有哪些副作用?"
2. 自问:"当前测试是否依赖这些副作用中的任何一个?"
3. 自问:"我是否完全理解这个测试的需求?"
如果测试依赖相关副作用:
在更低层级做Mock(针对实际的慢/外部操作)
或者使用能保留必要行为的测试替身
不要Mock测试依赖的高层方法
如果不确定测试依赖什么:
先使用真实实现运行测试
观察实际需要执行的逻辑
再在正确的层级添加最少的Mock
危险信号:
- "我先Mock一下保险"
- "这个可能很慢,还是Mock吧"
- 不理解依赖链就随意MockAnti-Pattern 4: Incomplete Mocks
反模式4:不完整的Mock
The violation:
typescript
// ❌ BAD: Partial mock - only fields you think you need
const mockResponse = {
status: 'success',
data: { userId: '123', name: 'Alice' }
// Missing: metadata that downstream code uses
};
// Later: breaks when code accesses response.metadata.requestIdWhy this is wrong:
- Partial mocks hide structural assumptions - You only mocked fields you know about
- Downstream code may depend on fields you didn't include - Silent failures
- Tests pass but integration fails - Mock incomplete, real API complete
- False confidence - Test proves nothing about real behavior
The Iron Rule: Mock the COMPLETE data structure as it exists in reality, not just fields your immediate test uses.
The fix:
typescript
// ✅ GOOD: Mirror real API completeness
const mockResponse = {
status: 'success',
data: { userId: '123', name: 'Alice' },
metadata: { requestId: 'req-789', timestamp: 1234567890 }
// All fields real API returns
};错误案例:
typescript
// ❌ BAD: Partial mock - only fields you think you need
const mockResponse = {
status: 'success',
data: { userId: '123', name: 'Alice' }
// Missing: metadata that downstream code uses
};
// Later: breaks when code accesses response.metadata.requestId错误原因:
- 局部Mock隐藏了结构假设 - 你只Mock了你知道的字段
- 下游代码可能依赖你没有包含的字段 - 出现静默失败
- 测试通过但集成失败 - Mock不完整,但真实API是完整的
- 虚假的安全感 - 测试完全无法验证真实行为
铁则: 要Mock完整的真实数据结构,而不是只Mock当前测试用到的字段。
修复方案:
typescript
// ✅ GOOD: Mirror real API completeness
const mockResponse = {
status: 'success',
data: { userId: '123', name: 'Alice' },
metadata: { requestId: 'req-789', timestamp: 1234567890 }
// All fields real API returns
};Gate Function
准入检查
BEFORE creating mock responses:
Check: "What fields does the real API response contain?"
Actions:
1. Examine actual API response from docs/examples
2. Include ALL fields system might consume downstream
3. Verify mock matches real response schema completely
Critical:
If you're creating a mock, you must understand the ENTIRE structure
Partial mocks fail silently when code depends on omitted fields
If uncertain: Include all documented fields创建Mock响应前:
确认:"真实API响应包含哪些字段?"
操作:
1. 查看文档/示例中的真实API响应
2. 包含系统下游可能用到的所有字段
3. 验证Mock完全匹配真实响应的Schema
关键:
如果你要创建Mock,就必须理解完整的结构
当代码依赖遗漏的字段时,局部Mock会出现静默失败
如果不确定:包含所有文档中列明的字段Anti-Pattern 5: Integration Tests as Afterthought
反模式5:集成测试被后置处理
The violation:
✅ Implementation complete
❌ No tests written
"Ready for testing"Why this is wrong:
- Testing is part of implementation, not optional follow-up
- TDD would have caught this
- Can't claim complete without tests
The fix:
TDD cycle:
1. Write failing test
2. Implement to pass
3. Refactor
4. THEN claim complete错误案例:
✅ 实现完成
❌ 未编写任何测试
"可以提测了"错误原因:
- 测试是实现的一部分,不是可选的后续环节
- TDD本可以避免这种情况
- 没有测试的功能不能称为完成
修复方案:
TDD流程:
1. 编写失败的测试
2. 实现功能让测试通过
3. 重构代码
4. 才能宣称功能完成When Mocks Become Too Complex
Mock过于复杂的情况
Warning signs:
- Mock setup longer than test logic
- Mocking everything to make test pass
- Mocks missing methods real components have
- Test breaks when mock changes
your human partner's question: "Do we need to be using a mock here?"
Consider: Integration tests with real components often simpler than complex mocks
警告信号:
- Mock的设置代码比测试逻辑还长
- 为了让测试通过Mock所有内容
- Mock缺少真实组件拥有的方法
- Mock改动时测试就会失败
你的搭档会这么问你: "我们这里真的需要用Mock吗?"
建议: 使用真实组件做集成测试通常比复杂的Mock更简单
TDD Prevents These Anti-Patterns
TDD可以避免这些反模式
Why TDD helps:
- Write test first → Forces you to think about what you're actually testing
- Watch it fail → Confirms test tests real behavior, not mocks
- Minimal implementation → No test-only methods creep in
- Real dependencies → You see what the test actually needs before mocking
If you're testing mock behavior, you violated TDD - you added mocks without watching test fail against real code first.
TDD的优势:
- 先写测试 → 强制你思考实际要测试的内容
- 观察测试失败 → 确认测试验证的是真实行为,而非Mock
- 最小化实现 → 不会混入仅测试用的方法
- 真实依赖 → Mock前你就能知道测试实际需要的内容
如果你在测试Mock的行为,说明你违反了TDD原则 - 你没有先在真实代码上看到测试失败,就提前添加了Mock。
Quick Reference
快速参考
| Anti-Pattern | Fix |
|---|---|
| Assert on mock elements | Test real component or unmock it |
| Test-only methods in production | Move to test utilities |
| Mock without understanding | Understand dependencies first, mock minimally |
| Incomplete mocks | Mirror real API completely |
| Tests as afterthought | TDD - tests first |
| Over-complex mocks | Consider integration tests |
| 反模式 | 修复方案 |
|---|---|
| 对Mock元素做断言 | 测试真实组件,或者取消Mock |
| 生产代码中存在仅测试用方法 | 移动到测试工具函数中 |
| 不理解依赖就Mock | 先理解依赖,最小化Mock |
| 不完整的Mock | 完全匹配真实API结构 |
| 测试被后置处理 | 遵循TDD,先写测试 |
| 过度复杂的Mock | 考虑使用集成测试 |
Red Flags
危险信号
- Assertion checks for test IDs
*-mock - Methods only called in test files
- Mock setup is >50% of test
- Test fails when you remove mock
- Can't explain why mock is needed
- Mocking "just to be safe"
- 断言校验格式的测试ID
*-mock - 方法只在测试文件中被调用
- Mock设置代码占测试代码的50%以上
- 删除Mock后测试就失败
- 说不清楚为什么需要Mock
- "只是为了保险"才Mock
The Bottom Line
总结
Mocks are tools to isolate, not things to test.
If TDD reveals you're testing mock behavior, you've gone wrong.
Fix: Test real behavior or question why you're mocking at all.
Mock是用于实现隔离的工具,而非测试的对象。
如果TDD过程中发现你在测试Mock的行为,说明你走偏了。
修复方法:测试真实行为,或者反思你为什么要做Mock。