testing-anti-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing 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.
测试必须验证真实行为,而非模拟行为。Mocks是用于隔离的手段,而非测试对象。
核心原则: 测试代码的实际功能,而非模拟的功能。
遵循严格的TDD可避免这些反模式。

The Iron Laws

铁律

1. NEVER test mock behavior
2. NEVER add test-only methods to production classes
3. NEVER mock without understanding dependencies
1. 绝不测试模拟行为
2. 绝不向生产类中添加仅测试用方法
3. 绝不在不理解依赖的情况下使用模拟

Anti-Pattern 1: Testing Mock Behavior

反模式1:测试模拟行为

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
// ❌ 错误:测试模拟是否存在
test('renders sidebar', () => {
  render(<Page />);
  expect(screen.getByTestId('sidebar-mock')).toBeInTheDocument();
});
问题原因:
  • 你在验证模拟是否可用,而非组件是否正常工作
  • 模拟存在时测试通过,不存在时测试失败
  • 无法反映真实行为的任何信息
人工检查提示: "我们是在测试模拟的行为吗?"
修复方案:
typescript
// ✅ 正确:测试真实组件或不使用模拟
test('renders sidebar', () => {
  render(<Page />);  // 不模拟侧边栏
  expect(screen.getByRole('navigation')).toBeInTheDocument();
});

// 或者如果必须模拟侧边栏以实现隔离:
// 不要对模拟做断言 - 测试侧边栏存在时Page的行为

Gate 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
在对任何模拟元素做断言之前:
  提问:"我是在测试真实组件的行为,还是仅测试模拟是否存在?"

  如果是测试模拟是否存在:
    停止 - 删除断言或取消组件模拟

  转而测试真实行为

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
// ❌ 错误:destroy()仅在测试中使用
class Session {
  async destroy() {  // 看起来像是生产API!
    await this._workspaceManager?.destroyWorkspace(this.id);
    // ... 清理操作
  }
}

// 在测试中
afterEach(() => session.destroy());
问题原因:
  • 生产类被仅测试用代码污染
  • 若在生产环境中意外调用会很危险
  • 违反YAGNI原则和关注点分离
  • 将对象生命周期与实体生命周期混淆
修复方案:
typescript
// ✅ 正确:由测试工具处理测试清理
// Session类中没有destroy() - 它在生产环境中是无状态的

// 在test-utils/
export async function cleanupSession(session: Session) {
  const workspace = session.getWorkspaceInfo();
  if (workspace) {
    await workspaceManager.destroyWorkspace(workspace.id);
  }
}

// 在测试中
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:在不理解依赖的情况下使用模拟

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
// ❌ 错误:模拟破坏了测试逻辑
test('detects duplicate server', () => {
  // 模拟阻止了测试依赖的配置写入操作!
  vi.mock('ToolCatalog', () => ({
    discoverAndCacheTools: vi.fn().mockResolvedValue(undefined)
  }));

  await addServer(config);
  await addServer(config);  // 应该抛出错误 - 但实际不会!
});
问题原因:
  • 被模拟的方法具有测试依赖的副作用(写入配置)
  • 为了“安全”过度模拟会破坏实际行为
  • 测试因错误的原因通过,或出现神秘的失败
修复方案:
typescript
// ✅ 正确:在合适的层级进行模拟
test('detects duplicate server', () => {
  // 仅模拟缓慢的部分,保留测试所需的行为
  vi.mock('MCPServerManager'); // 仅模拟缓慢的服务器启动过程

  await addServer(config);  // 配置已写入
  await addServer(config);  // 检测到重复项 ✓
});

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 chain
在模拟任何方法之前:
  停止 - 先不要模拟

  1. 提问:"真实方法有哪些副作用?"
  2. 提问:"这个测试是否依赖其中任何副作用?"
  3. 提问:"我是否完全理解这个测试的需求?"

  如果依赖副作用:
    在更低层级(实际缓慢/外部操作)进行模拟
    或使用保留必要行为的测试替身
    不要模拟测试依赖的高层级方法

  如果不确定测试依赖什么:
    先使用真实实现运行测试
    观察实际需要发生的操作
    然后在正确的层级添加最小化的模拟

  危险信号:
    - "我模拟这个只是为了安全"
    - "这个可能很慢,最好模拟它"
    - 在不理解依赖链的情况下进行模拟

Anti-Pattern 4: Incomplete Mocks

反模式4:不完整的模拟

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.requestId
Why 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
// ❌ 错误:部分模拟 - 仅包含你认为需要的字段
const mockResponse = {
  status: 'success',
  data: { userId: '123', name: 'Alice' }
  // 缺失:下游代码使用的metadata字段
};

// 后续问题:当代码访问response.metadata.requestId时会出错
问题原因:
  • 部分模拟隐藏了结构假设 - 你仅模拟了自己知道的字段
  • 下游代码可能依赖你未包含的字段 - 会出现静默失败
  • 测试通过但集成失败 - 模拟不完整,而真实API是完整的
  • 错误的信心 - 测试无法证明任何关于真实行为的内容
铁则: 模拟应完全匹配现实中存在的数据结构,而非仅包含当前测试使用的字段。
修复方案:
typescript
// ✅ 正确:完全匹配真实API的结构
const mockResponse = {
  status: 'success',
  data: { userId: '123', name: 'Alice' },
  metadata: { requestId: 'req-789', timestamp: 1234567890 }
  // 包含真实API返回的所有字段
};

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
在创建模拟响应之前:
  检查:"真实API响应包含哪些字段?"

  操作步骤:
    1. 查看文档/示例中的实际API响应
    2. 包含系统下游可能使用的所有字段
    3. 验证模拟完全匹配真实响应的 schema

  关键提示:
    如果你要创建模拟,必须理解整个结构
    当代码依赖缺失的字段时,部分模拟会静默失败

  如果不确定:包含所有文档中列出的字段

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

当模拟变得过于复杂时

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
警告信号:
  • 模拟设置的代码比测试逻辑更长
  • 为了让测试通过而模拟所有内容
  • 模拟缺少真实组件拥有的方法
  • 模拟变更时测试失败
人工检查提示: "我们这里真的需要使用模拟吗?"
建议: 使用真实组件的集成测试通常比复杂的模拟更简单

TDD Prevents These Anti-Patterns

TDD可避免这些反模式

Why TDD helps:
  1. Write test first → Forces you to think about what you're actually testing
  2. Watch it fail → Confirms test tests real behavior, not mocks
  3. Minimal implementation → No test-only methods creep in
  4. 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的作用:
  1. 先编写测试 → 迫使你思考实际要测试的内容
  2. 观察测试失败 → 确认测试针对的是真实行为,而非模拟
  3. 最小化实现 → 不会混入仅测试用方法
  4. 使用真实依赖 → 在模拟之前你会看到测试实际需要什么
如果你在测试模拟行为,说明你违反了TDD - 你在没有先观察测试在真实代码上失败的情况下就添加了模拟。

Quick Reference

快速参考

Anti-PatternFix
Assert on mock elementsTest real component or unmock it
Test-only methods in productionMove to test utilities
Mock without understandingUnderstand dependencies first, mock minimally
Incomplete mocksMirror real API completely
Tests as afterthoughtTDD - tests first
Over-complex mocksConsider integration tests
反模式修复方案
对模拟元素做断言测试真实组件或取消模拟
生产代码中的仅测试用方法移至测试工具中
在不理解依赖的情况下模拟先理解依赖,最小化模拟
不完整的模拟完全匹配真实API
测试作为事后补充TDD - 先写测试
过度复杂的模拟考虑使用集成测试

Red Flags

危险信号

  • Assertion checks for
    *-mock
    test IDs
  • 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"
  • 断言检查
    *-mock
    测试ID
  • 仅在测试文件中被调用的方法
  • 模拟设置代码占测试的比例超过50%
  • 移除模拟后测试失败
  • 无法解释为什么需要模拟
  • 模拟是“只是为了安全”

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.
模拟是用于隔离的工具,而非测试对象。
如果TDD显示你在测试模拟行为,说明你走错了方向。
修复方案:测试真实行为,或质疑自己为什么要使用模拟。