← 教程列表
coding

Anti-Pattern 1: Testing Mock Behavior

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

**The violation:** ```typescript // ❌ BAD: Testing that the mock exists test('renders sidebar', () => { render(); 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(); // 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 ```

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