为什么在测试DateTime相等时此单元测试失败?

时间:2021-12-15 16:25:06

Using NUnit 2.2 on .NET 3.5, the following test fails when using DateTime.Equals. Why?

在.NET 3.5上使用NUnit 2.2时,使用DateTime.Equals时,以下测试失败。为什么?

[TestFixture]
public class AttributeValueModelTest
{
    public class HasDate
    {
        public DateTime? DateValue
        {
            get
            {
                DateTime value;
                return DateTime.TryParse(ObjectValue.ToString(), out value) ? value : new DateTime?();
            }
        }

        public object ObjectValue { get; set; }
    }

    [Test]
    public void TwoDates()
    {
        DateTime actual = DateTime.Now;
        var date = new HasDate {ObjectValue = actual};
        Assert.IsTrue(date.DateValue.Value.Equals(actual));
    }
}

5 个解决方案

#1


14  

The dates aren't equal. TryParse drops some ticks. Compare the Tick values.

日期不相等。 TryParse会丢掉一些滴答声。比较Tick值。

For one test run:

对于一次测试运行:

Console.WriteLine(date.DateValue.Value.Ticks);
Console.WriteLine(actual.Ticks);

Yields:

633646934930000000
633646934936763185

#2


3  

The problem isn't really TryParse, but actually ToString().

问题不在于TryParse,实际上是ToString()。

A DateTime object starts with precision (if not accuracy) down to millionth of seconds. ToString() convertsit into a string, with precision only to a second.

DateTime对象以精确度(如果不是准确度)开始,下降到百万分之一秒。 ToString()将其转换为字符串,精度仅为一秒。

TryParse is doing the best it can with what it is given.

TryParse正在尽其所能地做到最好。

If you add a format specifier (along the lines of "yyyy-MM-dd HH:mm:ss.ffffff"), it should work.

如果你添加一个格式说明符(沿着“yyyy-MM-dd HH:mm:ss.ffffff”的行),它应该可以工作。

#3


1  

To specify a format that includes all the precision, you can use the String.Format() method. The example that James gives would look like this:

要指定包含所有精度的格式,可以使用String.Format()方法。詹姆斯给出的例子如下:

String.Format("{0:yyyy-MM-dd HH:mm:ss.ffffff}", ObjectValue);

I don't know what that will do when you pass it something that's not a date.

我不知道当你传递的东西不是约会时会做什么。

Perhaps a simpler approach is to add a special case when you've already got a date object:

也许更简单的方法是在已经有一个日期对象时添加一个特殊情况:

    public DateTime? DateValue
    {
        get
        {
            DateTime value = ObjectValue as DateTime;
            if (value != null) return value;
            return DateTime.TryParse(ObjectValue.ToString(), out value) ? value : new DateTime?();
        }
    }

#4


0  


public DateTime? DateValue
        {
            get
            {
                DateTime value;
                bool isDate = DateTime.TryParse(ObjectValue.ToString(), out value); 
                return isDate ? new DateTime?(value) : new DateTime?();
            }
        }

#5


0  

I don't know if this is the same in .NET, but in Java the equals often will only compare if the instances are the same, not if the values are the same. You'd instead want to use compareTo.

我不知道这在.NET中是否相同,但在Java中,equals通常只会在实例相同时进行比较,而不是在值相同的情况下进行比较。你想要使用compareTo。

#1


14  

The dates aren't equal. TryParse drops some ticks. Compare the Tick values.

日期不相等。 TryParse会丢掉一些滴答声。比较Tick值。

For one test run:

对于一次测试运行:

Console.WriteLine(date.DateValue.Value.Ticks);
Console.WriteLine(actual.Ticks);

Yields:

633646934930000000
633646934936763185

#2


3  

The problem isn't really TryParse, but actually ToString().

问题不在于TryParse,实际上是ToString()。

A DateTime object starts with precision (if not accuracy) down to millionth of seconds. ToString() convertsit into a string, with precision only to a second.

DateTime对象以精确度(如果不是准确度)开始,下降到百万分之一秒。 ToString()将其转换为字符串,精度仅为一秒。

TryParse is doing the best it can with what it is given.

TryParse正在尽其所能地做到最好。

If you add a format specifier (along the lines of "yyyy-MM-dd HH:mm:ss.ffffff"), it should work.

如果你添加一个格式说明符(沿着“yyyy-MM-dd HH:mm:ss.ffffff”的行),它应该可以工作。

#3


1  

To specify a format that includes all the precision, you can use the String.Format() method. The example that James gives would look like this:

要指定包含所有精度的格式,可以使用String.Format()方法。詹姆斯给出的例子如下:

String.Format("{0:yyyy-MM-dd HH:mm:ss.ffffff}", ObjectValue);

I don't know what that will do when you pass it something that's not a date.

我不知道当你传递的东西不是约会时会做什么。

Perhaps a simpler approach is to add a special case when you've already got a date object:

也许更简单的方法是在已经有一个日期对象时添加一个特殊情况:

    public DateTime? DateValue
    {
        get
        {
            DateTime value = ObjectValue as DateTime;
            if (value != null) return value;
            return DateTime.TryParse(ObjectValue.ToString(), out value) ? value : new DateTime?();
        }
    }

#4


0  


public DateTime? DateValue
        {
            get
            {
                DateTime value;
                bool isDate = DateTime.TryParse(ObjectValue.ToString(), out value); 
                return isDate ? new DateTime?(value) : new DateTime?();
            }
        }

#5


0  

I don't know if this is the same in .NET, but in Java the equals often will only compare if the instances are the same, not if the values are the same. You'd instead want to use compareTo.

我不知道这在.NET中是否相同,但在Java中,equals通常只会在实例相同时进行比较,而不是在值相同的情况下进行比较。你想要使用compareTo。