← 教程列表
coding

Problem 5: Mock-Interface Drift

📅 2026/6/12 16:30:02⏱ 约 5 分钟阅读👁 7 阅读

**What happened:** ```typescript // Interface defines close() interface PlatformAdapter { close(): Promise; } // Code (BUGGY) calls cleanup() await adapter.cleanup(); // Mock (MATCHES BUG) defines cleanup() vi.mock('web-adapter', () => ({ WebAdapter: vi.fn().mockImplementation(() => ({ cleanup: vi.fn().mockResolvedValue(undefined), // Wrong! })), })); ``` - Tests passed - Runtime crashed: "adapter.cleanup is not a function" **Root cause:** Mock derived from what buggy code calls, not from interface definition. TypeScript can't catch inline mocks with wrong method names. **Impact:** High - Tests give false confidence, runtime crashes **Why testing-anti-patterns didn't prevent this:** The skill covers testing mock behavior and mocking without understanding, but not the specific pattern of "derive mock from interface, not implementation." ---

← 返回教程列表 AI8899 · 内容仅供学习参考