与DateTime相关的测试仅在服务器上失败,但在本地环境中失败

时间:2015-10-28 14:52:57

标签: c# unit-testing datetime

我的TFS服务器上的单元测试失败,但在我的本地环境中运行正常。它正在测试这个函数,它只返回一个" time-before"基于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 = (comparingTo - 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"
        );
    }

这是我的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));
    }
}

但由于某种原因,该功能在服务器上无法正常运行:

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

为什么此功能在测试服务器上无法正常工作?我不知道它为什么会在0小时前说#34;当它看起来好像会说&#34; 1天前&#34;。测试在我的本地环境中正常通过,所以我无法对其进行调试。

1 个答案:

答案 0 :(得分:1)

您的TFS服务器运行测试比本地计算机快得多!更改测试以将oneDayAgo设置为此值,它将可靠地运行:

var oneDayAgo = DateTime.Now - TimeSpan.FromDays(1) - TimeSpan.FromSeconds(1);
                                                      ^^^^^^^^^^^^^^^^^^^^^^^^