【发布时间】:2018-11-22 15:27:22
【问题描述】:
我有映射方法,将模型A 映射到模型B。我为那里的所有人编写单元测试。当目标模型有很多属性时,我想以某种方式很好地处理情况,但其中大多数是null。测试的目的是测试一些属性是否正确映射,而其他属性则根本没有映射(除了设置了设计的属性),而无需指定每个属性。
我无法更改 B 类,因为我调用的通常是服务合同,其中只有少数属性。
能否请您帮助如何简化单元测试?
我正在使用 .NET Core 2.1 和 NUnit。
当前状态示例:
public class B
{
public string Id { get; set; }
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
public string Prop4 { get; set; }
public string Prop5 { get; set; }
// a lot of other properties
}
映射方法:
public B Map(A a)
{
return new B
{
Id = a.Id // only Id property is set
};
}
测试:
[Test]
public void MapTest()
{
var a = new A { Id = "123" };
var b = mapping.Map(a);
Assert.That(b, Is.Not.Null);
Assert.That(b.Id, Is.EqualTo(a.Id));
Assert.That(b.Prop1, Is.Null); // this is what i want to simplify
Assert.That(b.Prop2, Is.Null);
Assert.That(b.Prop3, Is.Null);
Assert.That(b.Prop4, Is.Null);
Assert.That(b.Prop5, Is.Null);
// a lot of other properties
}
【问题讨论】:
-
嗯,我看到的唯一选项是使用
Reflection,但我认为这并不能真正简化您的代码 -
我有这个想法,但正如你所说,这不是我正在寻找的解决方案类型。
-
你确定
B类的所有属性都是静态的吗? -
@Fabio,谢谢,这是一个错字。
标签: .net unit-testing testing .net-core nunit