2014-03-27 118 views
1

我做DTO的的比較和模型object.Like該做的斷言: -單元測試默認聲明方法?

Assert.AreEqual(_oCustomer.FKCustomerId, actual.FKCustomerId); 
Assert.AreEqual(_oCustomer.Name,actual.Name); 
Assert.AreEqual(_oCustomer.Description,actual.Description); 

但我想,而不是在每一個測試方法,我們可以對任何默認/自動功能做斷言爲每個屬性它呢?任何人都可以指導我嗎?

+1

你試過了什麼?嘗試在您喜歡的網頁搜索引擎中輸入「C#對象相等」。如果您有適當的相等比較器,您可以執行'Assert.AreEqual(_Customer,actual)'。 – CodeCaster

回答

1

如果不通過重寫Equals()來污染模型和/或命名空間以將模型與DTO進行比較,您可以簡單地創建一個方法,在每個測試中執行比較並調用它。現在,您仍然可以在每個屬性的基礎上進行斷言(這樣您就可以確切地看到哪一個正在打破測試),但您只能在一個地方更改它。

public static class ModelDtoComparer 
{ 
    public static void AssertAreEqual(Model model, Dto dto) 
    { 
     Assert.AreEqual(model.FKCustomerId, dto.FKCustomerId); 
     Assert.AreEqual(model.Name, dto.Name); 
     Assert.AreEqual(model.Description, dto.Description); 
     // etc. 
    } 
} 

評論響應
若要列出做到這一點,其中modelList應符合dtoList項目換項目:

Assert.AreEqual(modelList.Length, dtoList.Length); 

for (var i = 0; i < modelList.Length; i++) 
{ 
    ModelDtoComparer.AssertAreEqual(modelList[i], dtoList[i]); 
} 
+0

傑伊我怎麼能用這個對象列表(Dto列表和模型列表)? – Pawan

+0

@Pawan預計列表是否與項目對應項目相同?那麼,DTO列表中的第10個項目應該等於模型列表中的第10個項目? – Jay

+0

是的,我嘲笑的對象,所以它會返回相同的列表,我們將要或將傳遞到返回方法。 – Pawan

0

,如果你願意,你可以使用反射,這裏是一個通用方法,您可以使用它比較任何兩個對象,無論它們是什麼類型:

public void CompareMyObjects(object object1, object object2) 
{ 
    var type1Fields = object1.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty); 
    var type2Fields = object2.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty); 

    var propsInCommon = type1Fields.Join(type2Fields, t1 => t1.Name, t2 => t2.Name, (t1, t2) => new { frstGetter = t1.GetGetMethod(), scndGetter = t2.GetGetMethod() }); 

    foreach (var prop in propsInCommon) 
    { 
     Assert.AreEqual(prop.frstGetter.Invoke(object1, null), prop.scndGetter.Invoke(object2, null)); 
    } 
} 

,你可以按如下方式使用方法:

CompareMyObjects(actualCustomer, _oCustomer); 
CompareMyObjects(actualAccount, _account); 

我希望幫助。

1

你可以使用反射來寫某種比較器。這將比較給定兩個對象(僅適用於公共屬性)的Level1特性:

static void AssertAreEqual<T1, T2>(T1 instance1, T2 instance2) { 
    var properties1 = typeof(T1).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty); 
    var properties2 = typeof(T2).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty); 

    var properties = from p1 in properties1 
        join p2 in properties2 on 
        p1.Name equals p2.Name 
        select p1.Name; 
    foreach (var propertyName in properties) { 
    var value1 = properties1.Where(p => p.Name == propertyName).First().GetGetMethod().Invoke(instance1, null); 
    var value2 = properties2.Where(p => p.Name == propertyName).First().GetGetMethod().Invoke(instance2, null); 
    Assert.AreEqual(value1, value2); 
    } 
} 

public class PersonDto { 
    public string LastName { get; set; } 
    public int FieldFoo { get; set; } 
    public int Dto { get; set; } 
} 

public class PersonModel { 
    public string LastName { get; set; } 
    public int FieldFoo { get; set; } 
    public int Model { get; set; } 
} 

var p1 = new PersonDto { LastName = "Joe" }; 
var p2 = new PersonModel { LastName = "Joe" }; 
AssertAreEqual(p1, p2);