【发布时间】:2016-07-31 22:34:45
【问题描述】:
我正在尝试使用 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;}
}
Name 和 DateOfBirth 属性自定义不冲突,所以我不知道为什么 Name 最终为空。我希望名字是Ben。
我怎样才能得到它以便同时应用两个自定义项(即Name = "Ben" 和DateOfBirth = 1/1/1900)?
【问题讨论】:
标签: c# .net unit-testing autofixture