我的Assert.Equal()
对于一个匿名对象返回false,但是在调试器中手动检查属性一切都很好。
它没有抱怨每个说法的属性,只有以下(如果你在差异工具中比较完全相同)。
预期:{id = 1,name =,children = System.Collections.Generic.List
1[System.Collections.Generic.Dictionary
2 [System.String,System.String]] }(<> f__AnonymousType13[[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.List
1 [[System.Collections.Generic.Dictionary`2 [[System.String, mscorlib,版本= 4.0.0.0,文化=中性, PublicKeyToken = b77a5c561934e089],[System.String,mscorlib, Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]], mscorlib,版本= 4.0.0.0,文化=中性, PublicKeyToken = b77a5c561934e089]],mscorlib,Version = 4.0.0.0, Culture = neutral,PublicKeyToken = b77a5c561934e089]])
实际:{id = 1,name =,children = System.Collections.Generic.List
1[System.Collections.Generic.Dictionary
2 [System.String,System.String]] }(<> f__AnonymousType13[[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.List
1 [[System.Collections.Generic.Dictionary`2 [[System.String, mscorlib,版本= 4.0.0.0,文化=中性, PublicKeyToken = b77a5c561934e089],[System.String,mscorlib, Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]], mscorlib,版本= 4.0.0.0,文化=中性, PublicKeyToken = b77a5c561934e089]],mscorlib,Version = 4.0.0.0, Culture = neutral,PublicKeyToken = b77a5c561934e089]])
答案 0 :(得分:0)
阅读后:
Anonymous types unify within an assembly, Part One
Anonymous Types Unify Within An Assembly, Part Two
据我所知;如果匿名对象具有相同顺序的相同属性,则在程序集内,可以将它们作为相同类型进行比较。但是在单独的组件中,它们不能作为相同的类型进行比较。
经过大量的反复试验后,我反而使用CompareObjects open source project来更好地控制等式检查。这并不理想,因为我更喜欢坚持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);