一千萬個為什麽

搜索

比較MbUnit中兩個相同的對象

從MBUnit我試圖檢查兩個對象的值是否相同使用

Assert.AreSame(RawDataRow, result);

但是我得到以下失敗:

Expected Value & Actual Value : {RawDataRow: CentreID = "CentreID1",
CentreLearnerRef = "CentreLearnerRef1",
ContactID = 1, DOB = 2010-05-05T00:00:00.0000000,
Email = "Email1", ErrorCodes = "ErrorCodes1",
ErrorDescription = "ErrorDescription1", FirstName = "FirstName1"}

備註:格式化時兩個值看起來都相同,但它們是不同的實例。

我不想要經歷每個房產。我可以從MbUnit做到這一點嗎?

最佳答案

基本上, Assert.AreEqual 使用 Object.Equals()來驗證實際和預期實例之間的相等性,而 Assert.AreSame 使用<�代碼> Object.ReferenceEquals </代碼>。

如果你的類沒有實現任何內置的相等機制;例如,通過重寫 Object.Equals ,您將最終得到您描述的問題,因為 MbUnit 不知道如何比較被測類型的兩個實例。

There are several solutions. One of them is Coppermill's solution: you may want to implement a structural equality comparer based on reflection. But MbUnit has already a built-in feature like that. It's called StructuralEqualityComparer and it's very easy to use. So why reinventing the wheel?

Assert.AreSame(RawDataRow, result, new StructuralEqualityComparer
{
   { x => x.CentreID },
   { x => x.CentreLearnerRef, (a, b) => a.Equals(b, StringComparison.OrdinalIgnoreCase) },
   { x => x.ContactID },
  //You can customize completely the comparison process...
});

Anyway, I suggest you read that article: http://interfacingreality.blogspot.com/2009/09/equality-assertions-in-mbunit-v3.html

您可能還想閱讀Gallio wiki中的該文章

轉載註明原文: 比較MbUnit中兩個相同的對象