1

Assert.Equal()的匿名对象返回 false,但是在调试器中非常仔细地手动检查属性,一切似乎都很好。

它不会抱怨每个说的属性,只有以下内容(如果您在差异工具中进行比较,则完全相同)。

预期:{ id = 1, name = , children = System.Collections.Generic.List 1[System.Collections.Generic.Dictionary2[System.String,System.String]] } (<>f__AnonymousType1 3[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List1[[System.Collections.Generic.Dictionary`2[[System .String,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089],[System.String,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]],mscorlib,版本=4.0.0.0 , 文化=中性, PublicKeyToken=b77a5c561934e089]], mscorlib, 版本=4.0.0.0, 文化=中性, PublicKeyToken=b77a5c561934e089]])


实际:{ id = 1, name = , children = System.Collections.Generic.List 1[System.Collections.Generic.Dictionary2[System.String,System.String]] } (<>f__AnonymousType1 3[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List1[[System.Collections.Generic.Dictionary`2[[System .String,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089],[System.String,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]],mscorlib,版本=4.0.0.0 , 文化=中性, PublicKeyToken=b77a5c561934e089]], mscorlib, 版本=4.0.0.0, 文化=中性, PublicKeyToken=b77a5c561934e089]])

4

1 回答 1

0

看完之后:

匿名类型在程序集中统一,第 1 部分

匿名类型在程序集中统一,第二部分

据我所知;在一个程序集中,如果一个匿名对象以相同的顺序具有相同的属性,则可以将它们作为相同的类型进行比较。但是在单独的程序集中,它们不能作为同一类型进行比较。

经过大量试验和错误后,我改为使用CompareObjects 开源项目来更好地控制相等性检查。这并不理想,因为我更喜欢坚持 Assert ......但我需要在我的上下文中使用对象,所以这对于我的问题来说是一个非常优雅的解决方案。

 CompareObjects compareObjects = new CompareObjects();
 compareObjects.MaxDifferences = 1000;
 compareObjects.IgnoreObjectTypes = true; //handles anonymous types
 bool isSame = compareObjects.Compare(exepected, actual);
 Assert.True(isSame, compareObjects.DifferencesString);
于 2014-02-28T14:37:25.860 回答