How to test 11:30 at night at 2 in the afternoon
transcript
It's 2 in the afternoon. Your late-night fee only applies after 11 pm. How do you test it?
Wait until night? The build runs at noon. Sleep in the test until 11? Nine hours, for one check. Change the machine's time? Now everything else breaks. All three are workarounds. The real problem is in the code.
The checkout reads the machine's clock itself. So the total is 499 or 539, depending on when you run it. Give it a clock instead. The test decides the hour.
A Clock interface with one method: the hour. Production passes the system clock. The test passes a fixed clock, set to 23. The checkout never knows which one it got.
Hour 14: pays 499. No fee. Hour 23: 499 plus 40 is 539. Always. Instantly. Same code, any hour, and no real charge.
Don't read the clock inside the class with the rule. Pass it in. That's dependency injection.
Full breakdown in the lecture.