Hacker News new | ask | show | jobs
by rmetzler 4250 days ago
Once I recognized a certain test pass on all days except on Mondays.

The test created models for a few days and tested for a sum of the current week. The dates where all relative (today, yesterday, etc.), so when the test run on a Monday some records where for the last week and that's why the sum wasn't the same.

But I guess your problem is harder to debug.

1 comments

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.
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.