A stub is a test double that returns predetermined values for a dependency. A common mistake is testing code that calls time.Now() directly: timing-sensitive tests can become brittle or require sleeps. section 11.6
Inject a deterministic clock
Define type now func() time.Time and store it on Cache. Production construction assigns time.Now; the test assigns func() time.Time { return parseTime(t, "2020-01-01T12:00:00.06Z") }. TrimOlderThan then evaluates identical timestamps on every run.
Choose the dependency shape according to the API:
- Use a function type when the code needs only one operation, such as time.Now(). section 11.6
- Group related operations such as Now and After behind an interface when the dependency grows. section 11.6
- Avoid a mutable global clock when possible because shared state prevents isolated, parallel tests. section 11.6









