Lesson 38 — Mocking & Test Doubles (Visualization)
Trang này minh hoạ 3 ý chính của bài học: (1) phân biệt 5 loại test double, (2) luồng dependency injection production vs test, (3) chuỗi tương tác giữa test - service - mock.
1. 5 loại test double — chọn cho từng scenario
Chọn một scenario bên dưới — viz sẽ highlight loại double phù hợp nhất và giải thích vì sao.
Scenario:Chọn 1 scenario ở trên
2. Dependency Injection — wiring production vs test
Cùng NotifyService, cùng signature NewNotifyService(store UserStore, email EmailSender). Khác nhau ở chỗ "truyền cái gì vào".
3. Mock interaction trace — test → service → mock
Trace từng bước khi test chạy: .On() set expectation → service gọi mock → mock trả value → .AssertExpectations() verify.
🧪Test
TestNotifyUser
⚙️Service
NotifyService
🎭Mock
MockUserStore + MockEmail
Happy path: mock được khai báo
Fail path: service gọi
.On("Get", 42).Return(user), service gọi đúng → AssertExpectations pass.Fail path: service gọi
Get(99) trong khi expectation là Get(42) → AssertExpectations báo lỗi.