查什么CollectionAssert.AreEquivalent
What check CollectionAssert.AreEquivalent
我正在阅读 a MSDN article 中的方法 CollectionAssert.AreEquivalent
并且根据 MSDN:
Two collections are equivalent if they have the same elements in the same quantity, but in any order. Elements are equal if their values are equal, not if they refer to the same object.
我在 Visual Studio 中尝试了以下代码:
var first = new TradeData { ID = "A", MarketPrice = 0 };
var mockFir = new TradeData { ID = "A", MarketPrice = 0 };
var collection = new List<TradeData> { first };
var mockCollection = new List<TradeData> { mockFir };
CollectionAssert.AreEquivalent(collection, mockCollection);
但是我得到了一个例外:
CollectionAssert.AreEquivalent failed
所以,我的问题是:MSDN 说 "Elements are equal if their values are equal, not if they refer to the same object" 到底是什么意思?
因为 TradeData
class 没有覆盖 object.Equals
,基本实现接管了,它通过引用比较两个对象。尽管 first
和 mockFir
包含相同的值,但它们不是同一个对象,因此它们不被视为相等。如果您在 TradeData
class 中覆盖 Equals
,您的示例将起作用。
"Elements are equal if their values are equal, not if they refer to the same object"
It means that CollectionAssert.AreEquivalent uses Object.equals and not Object.ReferenceEquals. Depending on the Object type and implementation of Object.Equals, values will (rather "can") be used for comparison.
Object.Equals 对两种对象类型(释义)具有以下默认行为:
- 如果值类型相同并且它们的 public 和私有字段具有相同的值,则值类型相等。
- 当它们是同一个对象时,引用类型是相等的。对于引用类型,调用 Equals 等同于调用 ReferenceEquals。引用相等意味着对象变量引用同一个对象。
上述代码示例中的断言失败,因为您创建了不同的对象以插入到两个列表中。 AreEquivalent 使用 Object.Equals 方法比较对象。仅当指向同一对象时,equals 方法的默认实现才会 return 对 REference 类型对象为真。 要比较这些对象的实际值,您需要重写 TradeData 的 equals 方法。
令人遗憾的是,CollectionAssert.AreEqual 有另一种使用比较器的方法,但 CollectionAssert.AreEquivalent 却不是这样。
https://msdn.microsoft.com/en-us/library/ms243753.aspx
我正在阅读 a MSDN article 中的方法 CollectionAssert.AreEquivalent
并且根据 MSDN:
Two collections are equivalent if they have the same elements in the same quantity, but in any order. Elements are equal if their values are equal, not if they refer to the same object.
我在 Visual Studio 中尝试了以下代码:
var first = new TradeData { ID = "A", MarketPrice = 0 };
var mockFir = new TradeData { ID = "A", MarketPrice = 0 };
var collection = new List<TradeData> { first };
var mockCollection = new List<TradeData> { mockFir };
CollectionAssert.AreEquivalent(collection, mockCollection);
但是我得到了一个例外:
CollectionAssert.AreEquivalent failed
所以,我的问题是:MSDN 说 "Elements are equal if their values are equal, not if they refer to the same object" 到底是什么意思?
因为 TradeData
class 没有覆盖 object.Equals
,基本实现接管了,它通过引用比较两个对象。尽管 first
和 mockFir
包含相同的值,但它们不是同一个对象,因此它们不被视为相等。如果您在 TradeData
class 中覆盖 Equals
,您的示例将起作用。
"Elements are equal if their values are equal, not if they refer to the same object"
It means that CollectionAssert.AreEquivalent uses Object.equals and not Object.ReferenceEquals. Depending on the Object type and implementation of Object.Equals, values will (rather "can") be used for comparison.
Object.Equals 对两种对象类型(释义)具有以下默认行为:
- 如果值类型相同并且它们的 public 和私有字段具有相同的值,则值类型相等。
- 当它们是同一个对象时,引用类型是相等的。对于引用类型,调用 Equals 等同于调用 ReferenceEquals。引用相等意味着对象变量引用同一个对象。
上述代码示例中的断言失败,因为您创建了不同的对象以插入到两个列表中。 AreEquivalent 使用 Object.Equals 方法比较对象。仅当指向同一对象时,equals 方法的默认实现才会 return 对 REference 类型对象为真。 要比较这些对象的实际值,您需要重写 TradeData 的 equals 方法。
令人遗憾的是,CollectionAssert.AreEqual 有另一种使用比较器的方法,但 CollectionAssert.AreEquivalent 却不是这样。 https://msdn.microsoft.com/en-us/library/ms243753.aspx