Phân biệt thực tế: Stub cung cấp canned response không quan tâm đến cách gọi — jest.fn().mockReturnValue(user) để feed data cho SUT (system under test). Mock verify interactions — expect(emailService.send).toHaveBeenCalledWith(email) để đảm bảo behavior đúng. Fake có working implementation đơn giản hơn — in-memory repository thay vì real DB, fake email service lưu vào array.
- Hướng dẫn thực tế: dùng Stub khi test chỉ cần control input data (không care về side effects); dùng Mock khi hành động quan trọng phải xảy ra (email gửi, event emit, payment charge); dùng Fake khi cần nhiều interactions với dependency (5 operations trên DB) — fake in-memory repo nhanh và realistic hơn 5 mock calls.
- Over-mocking smell: nếu test cần mock > 3-4 dependencies, đó là dấu hiệu design issue (too many dependencies → god class).
- Test với real implementation khi có thể (fast enough, no external I/O) — integration > unit với mocks cho confidence.
Pitfall: mock quá nhiều → tests pass nhưng integration fails (mock không reflect thực tế API).