logoalt Hacker News

Alupisyesterday at 2:50 PM3 repliesview on HN

Just skimmed the PR, I'm sure the author knows more than I - but why hard code a date at all? Why not do something like `today + 1 year`?


Replies

CodesInChaosyesterday at 7:00 PM

That can easily lead to breaking tests due to time-zones, daylight saving time or the variable length of months.

We experienced several of those over the years, and generally it was the test that was wrong, not the code it was testing.

For example, this simplified test hits several of those pitfalls:

    var expected = start.AddMonths(1);
    var actual = start.ToLocal().AddMonths(1).ToUtc();
    Assert(expected == actual);
show 1 reply
johanvtsyesterday at 3:02 PM

That introduces dependency of a clock which might be undesirable, just had a similar problem where i also went for hardcoding for that reason.

show 2 replies
whynotmaybeyesterday at 3:02 PM

Because it should be `today + 1 year + randomInt(1,42) days`.

Always include some randomness in test values.

show 5 replies