断言.跨程序集的相等匿名对象失败 [英] Assert.Equal anonymous objects across assemblies fails

查看:89
本文介绍了断言.跨程序集的相等匿名对象失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My Assert.Equal() for an anonymous object is returning false, however very carefully inspecting the properties by hand in the debugger everything seems fine.

它只抱怨以下内容(如果您在比较工具中进行比较,则完全相同).

It doesn't complain about the properties per say, only the following (which if you compare in a diffing tool is exactly the same).

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

Expected: { id = 1, name = , children = System.Collections.Generic.List1[System.Collections.Generic.Dictionary2[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.List1[[System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, 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__AnonymousType1 3 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,版本= 4.0.0.0,文化=中性,PublicKeyToken = b77a5c561934e089],[System.Collections.Generic.List 1 [[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,Culture = neutral,PublicKeyToken = b77a5c561934e089]])

Actual: { id = 1, name = , children = System.Collections.Generic.List1[System.Collections.Generic.Dictionary2[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.List1[[System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]])

推荐答案

阅读后:

匿名类型在装配体(第一部分)中统一

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

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

From what I can tell; within an assembly if an anonymous object has the same properties in the same order, then they can be compared as the same type. But in separate assemblies they cannot be compared as the same type.

经过大量的试验和错误,我改用了 CompareObjects开源项目来进行更多控制平等检查.这不是理想的,因为我宁愿坚持使用Assert ...,但是我需要在上下文中使用对象,因此这对于我的问题而言是一个非常优雅的解决方案.

After lots of trial and error, I've instead used the CompareObjects open source project to have more control of the equality check. It's not ideal, as I'd prefer to stick to Assert... but I need to work with objects in my context, so this is pretty elegant solution for my problem.

 CompareObjects compareObjects = new CompareObjects();
 compareObjects.MaxDifferences = 1000;
 compareObjects.IgnoreObjectTypes = true; //handles anonymous types
 bool isSame = compareObjects.Compare(exepected, actual);
 Assert.True(isSame, compareObjects.DifferencesString);

这篇关于断言.跨程序集的相等匿名对象失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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