← 教程列表
news

Anti-Pattern 4: Incomplete Mocks

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

**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 }; ```

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