我有一组嵌套相当深的数据访问类。
AutoFixture 需要 2 分钟以上才能构建出其中 5 个的列表。每个单元测试 2 分钟太长了。
如果我手动编码它们,我只会编码我需要的那些,所以它会初始化得更快。有没有办法告诉 AutoFixture 只执行某些属性,这样它就不会花时间处理我不需要的结构区域?
例如:
public class OfficeBuilding
{
public List<Office> Offices {get; set;}
}
public class Office
{
public List<PhoneBook> YellowPages {get; set;}
public List<PhoneBook> WhitePages {get; set;}
}
public class PhoneBook
{
public List<Person> AllContacts {get; set;}
public List<Person> LocalContacts {get; set;}
}
public class Person
{
public int ID { get; set; }
public string FirstName { get; set;}
public string LastName { get; set;}
public DateTime DateOfBirth { get; set; }
public char Gender { get; set; }
public List<Address> Addresses {get; set;}
}
public class Addresses
{
public string Address1 { get; set; }
public string Address2 { get; set; }
}
有没有办法告诉 AutoFixture 为以下对象创建值:OfficeBuilding.Offices.YellowPages.LocalContacts
,但不去打扰OfficeBuilding.Offices.YellowPages.AllContacts
?
Nikos Baxevanis 提供的答案提供了各种基于约定的方法来回答这个问题。为了完整起见,您还可以进行更临时的构建:
var phoneBook = fixture.Build<PhoneBook>().Without(p => p.AllContacts).Create();
如果您希望您的 Fixture 实例始终执行此操作,您可以自定义它:
fixture.Customize<PhoneBook>(c => c.Without(p => p.AllContacts));
每次 Fixture 实例创建 PhoneBook 实例时,它都会跳过 AllContacts 属性,这意味着您可以执行以下操作:
var sut = fixture.Create<OfficeBuilding>();
并且 AllContacts 属性将保持不变。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)