问题描述
我有一组嵌套相当深的数据访问类的。
I have a set of Data Access classes that are nested fairly deep.
要构建的5人名单需要AutoFixture超过2分钟。每个单元测试2分钟,路长。
To construct a list of 5 of them takes AutoFixture more than 2 minutes. 2 minutes per Unit test is way to long.
如果我采用手动编码方式,我只会code了那些我所需要的,所以它会初始化更快。有没有办法告诉AutoFixture只做一些属性,因此它不能花时间与我的结构领域,我不需要?
If I was coding them by hand, I would only code up the ones I need, so it would initialize quicker. Is there a way to tell AutoFixture to only do some of the properties so it can not spend time with areas of my structure I don't need?
例如:
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
?
Is there a way to tell AutoFixture to create values for OfficeBuilding.Offices.YellowPages.LocalContacts
, but not to bother with OfficeBuilding.Offices.YellowPages.AllContacts
?
推荐答案
一种选择是创建一个自定义,省略某个名字的属性:
One option is to create a Customization that omits properties of a certain name:
internal class PropertyNameOmitter : ISpecimenBuilder
{
private readonly IEnumerable<string> names;
internal PropertyNameOmitter(params string[] names)
{
this.names = names;
}
public object Create(object request, ISpecimenContext context)
{
var propInfo = request as PropertyInfo;
if (propInfo != null && names.Contains(propInfo.Name))
return new OmitSpecimen();
return new NoSpecimen(request);
}
}
您可以使用它,如下:
var fixture = new Fixture();
fixture.Customizations.Add(
new PropertyNameOmitter("AllContacts"));
var sut = fixture.Create<OfficeBuilding>();
// -> The 'AllContacts' property should be omitted now.
另请参阅:
See also:
- Omit properties by type
- Omit properties by namespace
这篇关于我怎么能指示AutoFixture不打扰填写一些属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!