ShouldBeEquivalentTo失败的等效对象时,主题是一个DateTime [英] ShouldBeEquivalentTo failing for equivalent objects when the subject is a DateTime

查看:169
本文介绍了ShouldBeEquivalentTo失败的等效对象时,主题是一个DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做

我刚刚建立了一个测试,以确保NodaTime LocalDateTime 映射到.NET 的DateTime ,保持相同的日期和时间值。我使用FluentAssertions' ShouldBeEquivalentTo 的方法来比较相应的属性值。

  [TestClass的]
公共类LocalDateTimeMapping
{
    [测试方法]
    公共无效RetainsComponentValues​​()
    {
        VAR nodatimeTime =新LocalDateTime();
        变种dotnetTime = nodatimeTime.ToDateTimeUnspecified();

        dotnetTime.ShouldBeEquivalentTo(nodatimeTime,
            O => Ø
                。包括(D => d.Year)
                。包括(D => d.Month)
                。包括(D => d.Day)
                。包括(D => d.Hour)
                。包括(D => d.Minute)
                。包括(D => d.Second)
                。包括(D => d.Millisecond)
        );
    }
}
 

问题

测试失败:

 期望的对象是01/01/1970 00:00:00,却发现与LT; 1970-01-01取代。

随着配置:
 - 选择属性System.DateTime.Year
 - 选择属性System.DateTime.Month
 - 选择属性System.DateTime.Day
 - 选择属性System.DateTime.Hour
 - 选择属性System.DateTime.Minute
 - 选择属性System.DateTime.Second
 - 选择属性System.DateTime.Millisecond
 - 按名称(或掷)匹配性
 - 调用操作< D​​ateTime的>当info.RuntimeType.IsSameOrInherits(的System.DateTime)
 - 调用操作<字符串> info.RuntimeType.IsSameOrInherits时(System.String)
 

我不知道是什么,最后两行的意思。

我已经试过

  1. 运行测试在调试器,以确认每个这些值是相同的。
  2. 看到,如果问题是通过删除包含 S为不同属性的特定属性。
  3. 在此基础上 EquivalencyAssertionOptions&LT的配置,日期时间> .Empty(),以确保没有额外的检查和性能进行了暗中参与
  4. 简化了这一切,只是下面的内容。

      dotnetTime.ShouldBeEquivalentTo(nodatimeTime,
        O => EquivalencyAssertionOptions< D​​ateTime的> .Empty()
    );
     

解决方案

从错误信息下面这行指出,某种特殊的治疗被给予的DateTime

 调用操作< D​​ateTime的>当info.RuntimeType.IsSameOrInherits(的System.DateTime)
 

我试着换了两个日期,时间,我比较了,这解决了这个问题:

  nodatimeTime.ShouldBeEquivalentTo(dotnetTime,
    O => Ø
        。包括(D => d.Year)
        。包括(D => d.Month)
        。包括(D => d.Day)
        。包括(D => d.Hour)
        。包括(D => d.Minute)
        。包括(D => d.Second)
        。包括(D => d.Millisecond)
);
 

所以,我的结论是 ShouldBeEquivalentTo 不应该叫上一个.NET 的DateTime

What I'm trying to do

I've just set up a test to ensure that a NodaTime LocalDateTime is mapped to a .NET DateTime, retaining the same date and time values. I'm using FluentAssertions' ShouldBeEquivalentTo method to compare the corresponding property values.

[TestClass]
public class LocalDateTimeMapping
{
    [TestMethod]
    public void RetainsComponentValues()
    {
        var nodatimeTime = new LocalDateTime();
        var dotnetTime = nodatimeTime.ToDateTimeUnspecified();

        dotnetTime.ShouldBeEquivalentTo(nodatimeTime,
            o => o
                .Including(d => d.Year)
                .Including(d => d.Month)
                .Including(d => d.Day)
                .Including(d => d.Hour)
                .Including(d => d.Minute)
                .Including(d => d.Second)
                .Including(d => d.Millisecond)
        );
    }
}

The problem

The test is failing:

Expected subject to be 01/01/1970 00:00:00, but found <1970-01-01>.

With configuration:
- Select property System.DateTime.Year
- Select property System.DateTime.Month
- Select property System.DateTime.Day
- Select property System.DateTime.Hour
- Select property System.DateTime.Minute
- Select property System.DateTime.Second
- Select property System.DateTime.Millisecond
- Match property by name (or throw)
- Invoke Action<DateTime> when info.RuntimeType.IsSameOrInherits(System.DateTime)
- Invoke Action<String> when info.RuntimeType.IsSameOrInherits(System.String)

I don't know what the last two lines mean.

What I've tried

  1. Running the test in the debugger to confirm that the values of each of these were the same.
  2. Seeing if the problem was a specific property by removing the Includes for the different properties.
  3. Basing the configuration on EquivalencyAssertionOptions<DateTime>.Empty() to ensure that no extra checks or properties were implicitly involved.
  4. Simplifying it all down to just the following.

    dotnetTime.ShouldBeEquivalentTo(nodatimeTime,
        o => EquivalencyAssertionOptions<DateTime>.Empty()
    );
    

解决方案

The following line from the error message indicated that some sort of special treatment was being given to the DateTime:

Invoke Action<DateTime> when info.RuntimeType.IsSameOrInherits(System.DateTime)

I tried swapping the two date-times I was comparing, and this resolved the problem:

nodatimeTime.ShouldBeEquivalentTo(dotnetTime,
    o => o
        .Including(d => d.Year)
        .Including(d => d.Month)
        .Including(d => d.Day)
        .Including(d => d.Hour)
        .Including(d => d.Minute)
        .Including(d => d.Second)
        .Including(d => d.Millisecond)
);

So I conclude that ShouldBeEquivalentTo should not be called on a .NET DateTime.

这篇关于ShouldBeEquivalentTo失败的等效对象时,主题是一个DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆