单元测试DateTime逻辑

时间:2018-05-09 17:20:48

标签: c# unit-testing

public bool IsBoundaryDate(DateTime deleteDate)
{
    var isBoundaryDate = false; 

    var daysList = new List<int>{1};

    foreach(var boundary in daysList )
    {
        var daysLeft = (deleteDate - DateTime.UtcNow).Days;

        if (daysLeft == boundary)
        {
            isBoundaryDate = true;
            break;
        }
    }

    return isBoundaryDate;
}

因此,在进行单元测试时,我的代码如下所示:

public void IsBoundaryDate_ReturnFalse_WhenDateIsNotInBoundaryDays()
{
    var boundaryDaysFake = new List<int>
    {
        1
    };

    var deleteDate = DateTime.UtcNow.AddDays(1).AddMinutes(1);
    var sut = new BoundaryDateChecker(boundaryDaysFake);

    Assert.IsTrue(sut.IsBoundaryDate(deleteDate));
}

如何避免硬编码AddMinutes(1),因为硬编码对于测试方法来说似乎是一个坏主意

0 个答案:

没有答案