我正在为 DateTimeUTC 进行 .NET 5 单元测试。我的以下测试以毫秒为单位失败。我正在初始化expectedDateTimeUtcNow = DateTimeOffset.UtcNow,然后使用expectedDateTimeUtcNow 转换为毫秒作为SUT 来测试我的方法,我通过硬编码的长毫秒值然后测试通过不确定我缺少什么
[Fact]
public void ConvertDateTimeToUTC_MustTakeDateTimeParameter_InMilliSecond()
{
//Arrange
long startDateTimeUtc = 1626991200000; //2021-07-22 22:00:00
var expectedDateTime = DateTimeOffset.FromUnixTimeMilliseconds(startDateTimeUtc);
var expectedDateTimeUtcNow = DateTimeOffset.UtcNow;
var dateTimeUtcNowMiliSec = expectedDateTimeUtcNow.ToUnixTimeMilliseconds();
//Act
DateTimeOffset utcConvertedDateTime = DateTimeCalculationHelper.ConvertDateTimeToUTC(startDateTimeUtc);
DateTimeOffset actualDateTimeUtcNow = DateTimeCalculationHelper.ConvertDateTimeToUTC(dateTimeUtcNowMiliSec);
//Assert
Assert.Equal(expectedDateTime, utcConvertedDateTime);
Assert.Equal(expectedDateTimeUtcNow, actualDateTimeUtcNow);
}
苏特
public static DateTimeOffset ConvertDateTimeToUTC(long utcDateTimeInMilliSec)
{
var dateTimeUtc = DateTimeOffset.FromUnixTimeMilliseconds(utcDateTimeInMilliSec);
return dateTimeUtc;
}