Hacker News new | ask | show | jobs
by ngzhian 4254 days ago
Tests that relied on date (especially involving getting the current date) are usually hard to get right. I had an assignment in school to build a To-do list, and had lots of problems trying to test it. I think I solved it by having methods take 2 dates, initial and relative date, rather than just getting the current date.
1 comments

Yes, a method is usually much easier to test, if the method gets the current date passed as an argument. The same goes for methods calculating on random values. Passing these to the method enables testing.