为集成测试创建对象(域模型)的最佳 generic/automated 方法是什么?

What's the best generic/automated approach to create objects (domain models) for integration tests?

我想为多层应用程序创建易于维护的集成测试,但我 运行 遇到了 找到创建对象的灵活方法 的问题。此外,我想保留有时控制对象创建的可能性。

在我的测试中,我创建了我的对象(域模型),将它们保存在数据库中,在我的 odata 控制器的响应中检索由域逻辑过滤的对象,将它们序列化回我的域模型,并将它们与预期返回的内容。

到目前为止我尝试的是在我的场景中结合使用 Specflow 的数据表和 FizzWare NBuilder,但是我已经看到维护测试并不方便,因为领域模型中的任何微小变化都可能导致需要更新集成测试项目的多个区域(在这种情况下,NBuilder 对象配置和提供更改模型数据的每个 specflow 场景),因为我的应用程序的域模型和逻辑很复杂。

是否有任何通用的自动化方法可以实现此目的?

分享你对此事的knowledge/experience。

我会写评论,但我的声誉不允许我这样做。

查看 Autofixture 库。它是由 Mark Seemann 创建的一个开源库,提供了一种自动创建带有用于测试目的的假数据的对象的方法:

https://github.com/AutoFixture/AutoFixture

这是一个非常简单的例子

public class Person
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", FirstName, Lastname);
        }
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void FullNameProperlyResolved()
    {
        var fixture = new Fixture();
        var sut = fixture.Create<Person>();
        var expectedFullName = string.Format("{0} {1}", sut.FirstName, sut.Lastname); 
        Assert.AreEqual(expectedFullName, sut.FullName);
    }
}