繁体   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