Hacker News new | past | comments | ask | show | jobs | submit login

Could you expand more on not using now()



Using the real-time clock makes your test impossible to reproduce. Using a mock clock leads to reproducible tests. Same with calls to PRNGs: these should be injected so your test failures can be reproduced.


Your now() in tests depends on current moment.

You cannot test how your code behaves when midnight passes, when daylight saving time changes either way, when it's 29th of February, etc.

More importantly, you cannot reliably (let alone quickly) test how your retry and timeout handling works, especially if two threads interact, or when you do (stubbed or mocked) remote API calls. All these things are the daily bread when working with Google's services, and basically at any setup with multiple (micro)services.


The first time I implemented an "IDateTimeService" in one of our applications, I was greeted with disbelief by my colleagues. Once I challenged them in running tests for certain edge cases, their mind shifted from "well, we cannot test that then" to "makes totally sense".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: