2013-01-24 45 views
2

因此,我在我的應用程序中有一層將object的一種類型映射到另一個類型中。認爲ViewModel模型類型的映射。 ViewModel可能具有名稱不同或模型中不存在的屬性。反之亦然。斷言2個對象相同

我想測試我的映射層,比較分配,但也允許我提供一些不同的屬性的邊緣案例處理。理想情況下,如果未檢查ViewModel中的所有屬性,則測試將失敗。

有誰知道這樣的野獸是否已經存在?

public class CustomerViewModel 
{ 
    // This is the same as CustomerModel.CustomerName, but the names differ 
    public string Name { get; set; } 
    public int ID { get; set; } 
} 

public class CustomerModel 
{ 
    public string CustomerName { get; set; } 
    public int ID { get; set; } 
} 

// Would auto test the properties that match automatically. Additionaltest test for non matching. Fails if all properties aren't tested 
Assert.CompareObjects(customerViewModelInstance, customerModelInstance) 
    .AdditionalTest("Name", "CustomerName") 
    .AdditionalComplexText((viewModel, model) => 
      { 
       // do some sort of a compare of complex objects. Maybe the viewmodel has address fields, address1, address2 while the Model has an Address object with those fields. 
      }); 

這背後的驅動力是必須在代碼中手動斷言每一個屬性一個非常大的應用程序的艱鉅任務。

+0

「這樣的野獸」會如何知道你正在比較哪些屬性與哪些屬性相比?除此之外,我認爲你能夠以自動化的方式做的最多的事情就是比較每個班級之間的*數量*屬性,以確保他們是平等的;但這並不能證明所有的任務都存在。 –

+0

@Kyralessa。請看我的答案。有一定的方法來比較實際的屬性。 –

+2

'IEquatable ' –

回答

2

你會需要重寫.Equals()使得它相比properties,然後用

Assert.AreEqual Method (Object, Object)。請看下面:

Compare equality between two objects in NUnit

你想實現你的模型本身這樣的事情。

// useful class 
public class MyStuff 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int MyValue { get; set; } 

    public override int GetHashCode() 
    { 
     return Id; 
    } 

    public override bool Equals(object obj) 
    { 
     if (ReferenceEquals(this, obj)) return true; 
     if (obj.GetType() != typeof (MyStuff)) return false; 

     var other = obj as MyStuff; 

     return (other.Id == Id 
      && other.MyValue == MyValue 
      && other.Equals(other.Name, Name)); 
     // use .Equals() here to compare objects; == for Value types 

     // alternative weak Equals() for value objects: 
     // return (other.MyValue == MyValue && other.Equals(other.Name, Name)); 
    } 
} 

編輯: 回想起來,我已經決定,在你的視圖模型和模型屬性的重複可能是一個壞的模式,是你有這麼多的測試問題的部分原因。相反,你應該允許你的ViewModel包裝你的模型。

public class CustomerViewModel 
{ 
    // This is the same as CustomerModel.CustomerName, but the names differ 
    public CustomerModel CustomerModel { get; set; } 

    pubiic CustomerViewModel() 
    { 
     CustomerModel = new CustomerModel(); 
    } 
} 

public class CustomerModel 
{ 
    public string CustomerName { get; set; } 
    public int ID { get; set; } 
} 

在這一點上,它更容易,以測試它,因爲你有你的包裹模型,你可以比較同型號的新副本,使用.Equals覆蓋模式。在一天結束的時候,我不認爲試圖想出一個「將任何模型與任何模型進行比較」的魔力子彈是一個好主意,也不實際。

+0

作爲旁註,覆蓋equals可能會導致一些頭痛的問題。除非你需要使用'Equals'方法,否則我會建議創建一個新方法'CustomEquals'方法並調用它,這樣你就可以判斷2個對象是否完全重複。 –

+0

在這種情況下,等於的簡單覆蓋是一個壞主意。我不應該爲了支持單元測試而改變一個類並且緊密地將一種類型耦合到另一種類型。比較需要在類之外。 – Darthg8r

+0

@ Darthg8r我明白你的關心。也就是說,我不確定你的這個野獸是如何根據你的要求來建造的。你會採取兩個類,拉出所有屬性,然後匹配基礎類型,然後比較?如果名稱不同,您如何確定您正在比較正確的屬性?我不確定這可能會被取消。它開始看起來像你可能不得不做手動比較。 –