mercredi 28 octobre 2015

DateTime-related test only fails on server, but not on local environment

I have a unit test that is failing on our TFS server, but is running just fine on my local environment. It is testing this function, which just returns a "time-ago" string based on a DateTime:

    /// <summary>
    /// Formats a DateTime object into a relative "time-ago" string.
    /// </summary>
    /// <param name="datetime">The DateTime to format.</param>
    /// <returns>Returns a formatted "time-ago" string.</returns>
    public static string GetRelativeTimeAgo(DateTime datetime)
    {
        return GetRelativeTimeAgo(datetime, DateTime.Now);
    }

    /// <summary>
    /// Formats a DateTime object into a relative "time-ago" string.
    /// </summary>
    /// <param name="datetime">The DateTime to format.</param>
    /// <param name="comparingTo">The DateTime to compare to.</param>
    /// <returns>Returns a formatted "time-ago" string.</returns>
    public static string GetRelativeTimeAgo(DateTime datetime, DateTime comparingTo)
    {
        TimeSpan ts = (DateTime.Now - datetime).Duration();
        Tuple<int, string> result;

        if (ts <= TimeSpan.FromMinutes(60))
            result = new Tuple<int, string>(ts.Minutes, "minute");
        else if (ts <= TimeSpan.FromHours(24))
            result = new Tuple<int, string>(ts.Hours, "hour"); 
        else if (ts <= TimeSpan.FromDays(30))
            result = new Tuple<int, string>(ts.Days, "day");
        else if (ts <= TimeSpan.FromDays(365))
            result = new Tuple<int, string>(ts.Days / 30, "month");
        else
            result = new Tuple<int, string>(ts.Days / 365, "years");

        return string.Format
        (
            "{0} {1}{2} {3}",
            result.Item1,
            result.Item2,
            result.Item1 > 1 ? "s" : "",
            datetime > comparingTo ? "in the future" : "ago"
        );
    }

Here is my TestClass:

[TestClass]
public class DateUtilityTests
{
    [TestMethod]
    public void TestRelativeTimeAgo()
    {
        var oneDayAgo = DateTime.Now - TimeSpan.FromDays(1);
        var twoDaysAgo = DateTime.Now - TimeSpan.FromDays(2);
        var oneMonthAgo = DateTime.Now - TimeSpan.FromDays(32);

        Assert.AreEqual("1 day ago", DateUtility.GetRelativeTimeAgo(oneDayAgo, DateTime.Now)); // this is line 20
        Assert.AreEqual("2 days ago", DateUtility.GetRelativeTimeAgo(twoDaysAgo, DateTime.Now));
        Assert.AreEqual("1 month ago", DateUtility.GetRelativeTimeAgo(oneMonthAgo, DateTime.Now));
    }
}

Yet for some reason, the function does not work properly on the server:

2015-10-28T14:45:03.0408380Z Failed   TestRelativeTimeAgo
2015-10-28T14:45:03.0564516Z ##[error]Error Message:
2015-10-28T14:45:03.0564516Z ##[error]   Assert.AreEqual failed. Expected:<1 day ago>. Actual:<0 hour ago>. 
2015-10-28T14:45:03.0564516Z ##[error]Stack Trace:
2015-10-28T14:45:03.0564516Z ##[error]   at ThriftbooksBLTests.General.DateUtilityTests.TestRelativeTimeAgo() in C:\...\General\DateUtilityTests.cs:line 20

Why is this function not working correctly on the test server? I have no idea why it says "0 hour ago", when it seems like it would say "1 day ago". The test passes just fine on my local environment, so I can't really debug it.

Aucun commentaire:

Enregistrer un commentaire