.net 当主题为DateTime时,应等效到等效对象失败 我想做什么

.net 当主题为DateTime时,应等效到等效对象失败 我想做什么,.net,datetime,nodatime,fluent-assertions,.net,Datetime,Nodatime,Fluent Assertions,我刚刚设置了一个测试,以确保NodaTimeLocalDateTime映射到.NETDateTime,并保留相同的日期和时间值。我正在使用FluentAssertions的shouldbeeeequivalentto方法来比较相应的属性值 [TestClass] public class LocalDateTimeMapping { [TestMethod] public void RetainsComponentValues() { var nodati

我刚刚设置了一个测试,以确保NodaTime
LocalDateTime
映射到.NET
DateTime
,并保留相同的日期和时间值。我正在使用FluentAssertions的
shouldbeeeequivalentto
方法来比较相应的属性值

[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)
        );
    }
}
问题 测试失败:

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)

错误消息中的以下行表示正在对
日期时间
进行某种特殊处理:

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

因此我得出结论,
应该等价于
不应该在.NET上调用
日期时间

您可以将日期时间与流畅的断言进行比较,他们现在添加了很多很好的方法来链接它:

e、 g


我不熟悉FluentAssertions。。。遗憾的是,它没有显示哪些属性不同(或者还有什么其他错误)。有没有办法让它提供更多的信息?@JonSkeet,我想它通常会给你一个特定的错误。似乎这种特殊情况可能是个例外。shouldbeeequivalentto()用于比较复杂的对象图,而不是.NET framework的基本类型部分。异常报告末尾的特定操作意味着对于DateTime属性,它将使用内置的DateTime特定断言。显然,其中一个选定属性的类型是DateTime。@DennisDoomen,在我问题中的代码中,没有一个选定属性的类型是
DateTime
。这是否意味着FluentAssertions中存在错误,或者我误解了您的意思?没有,但您是在对DateTime而不是复杂类型调用shouldbeeEquivalentto()方法。我从来没有打算支持这个。链接不再可用。也许你可以用
with autoconversion
选项来解决这个问题?或者Fluent断言5.0版可以解决这个问题?
Invoke Action<DateTime> when info.RuntimeType.IsSameOrInherits(System.DateTime)
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)
);
theDatetime.Should().BeLessThan(10.Minutes()).Before(otherDatetime);