相关疑难解决方法(0)

如何在保持类型自定义的同时使用AutoFixture构建自定义属性?

我试图使用autofixture来创建一个对象,但它们是我想要始终默认的某些属性(其余的可以自动生成).但是,每当我设置自定义时,它都会在我使用自定义构建时被覆盖.

void Main()
{
    var fixture = new Fixture();
    fixture.Customize<Person>(composer => composer.With(p => p.Name, "Ben"));

    var person = fixture.Build<Person>()
        .With(p => p.DateOfBirth, new DateTime(1900, 1, 1))
        .Create();

    /*  RESULT OF person below
    Name    null
    DateOfBirth 1/1/1900
    StreetAddress   StreetAddressafd6b86b-376a-4355-9a9c-fbae34731453
    State   State019e867b-ac5e-418f-805b-a64146bc06bc
    */
}

public class Person
{
    public string Name { get; set;}

    public DateTime DateOfBirth { get; set;}

    public string StreetAddress { get; set;}

    public string State { get; set;}
}
Run Code Online (Sandbox Code Playgroud)

"Name"和"DateOfBirth"属性自定义不会发生冲突,因此我不知道为什么Name最终为null.我希望名字是"本".

如何获得它以便应用两种自定义(即Name ="Ben"和DateOfBirth = 1/1/1900?

.net c# unit-testing autofixture

14
推荐指数
2
解决办法
8898
查看次数

标签 统计

.net ×1

autofixture ×1

c# ×1

unit-testing ×1