簡體   English   中英

單元測試默認的斷言方法?

[英]Unit test default Assertion method?

我正在通過比較dto和模型對象來進行斷言。

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

但是我想,不是對每個測試方法中的每個屬性都進行斷言,我們可以為其提供任何默認/自動功能嗎? 有人可以指導我嗎?

通過覆蓋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 item-for-item相匹配:

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

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

您可以使用反射來編寫某種比較器。 這將比較給定兩個對象的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);

您可以根據需要使用反射,這是一種通用方法,可以用來比較任何兩個對象,無論它們是什么類型:

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);

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM