简体   繁体   English

防止Autofixture填充子集合

[英]Preventing Autofixture from filling child collections

I'm using the latest version of Autofixture, and I'd like to prevent it from filling automatically child collections. 我正在使用最新版本的Autofixture,我想阻止它自动填充子集合。

For example, I have a class Person that has a List property. 例如,我有一个具有List属性的Person类。 I want all properties filled, except the list. 我想要填写所有属性,列表除外。

I tried using this customization : 我尝试使用此自定义:

public class RemoveMultiples : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations
            .OfType<FilteringSpecimenBuilder>()
            .Where(x => x.Specification is DictionarySpecification)
            .ToList().ForEach(c => fixture.Customizations.Remove(c));
        fixture.Customizations
            .OfType<FilteringSpecimenBuilder>()
            .Where(x => x.Specification is CollectionSpecification)
            .ToList().ForEach(c => fixture.Customizations.Remove(c));
        fixture.Customizations
            .OfType<FilteringSpecimenBuilder>()
            .Where(x => x.Specification is HashSetSpecification)
            .ToList().ForEach(c => fixture.Customizations.Remove(c));
        fixture.Customizations
            .OfType<FilteringSpecimenBuilder>()
            .Where(x => x.Specification is ListSpecification)
            .ToList().ForEach(c => fixture.Customizations.Remove(c));
    }
}

But it also prevents me from using .CreateMany() . 但它也阻止我使用.CreateMany()

edit: I can use .CreateMany(3) and it works. 编辑:我可以使用.CreateMany(3) ,它的工作原理。

Is there a setting somewhere that could let me autofill collections only when I need to? 是否有某个设置可以让我只在我需要时自动填充集合?

edit2: Class person should look like this: edit2:班级人员应如下所示:

[Serializable]
public class Person
{
    private ICollection<OtherClass> _otherClasses; 
    private string _something;
    public virtual ICollection<OtherClass> OtherClasses
    {
        get { return _otherClasses; }
        set { _otherClasses = value; }
    }
}

Note that it's not always a Collection , but sometimes IList 请注意,它并不总是一个Collection ,但有时是IList

Note2: I just realized that someone also removed the Customization for IEnumerable hence why the CreateMany() doesn't create anything. 注意2:我刚才意识到有人也删除了IEnumerable的Customization,因此CreateMany()为什么不创建任何东西。

Here's one way to do it. 这是一种方法。

Start by implementing a SpecimenBuilder that tells AutoFixture to skip assigning a value for collection property: 首先实现一个SpecimenBuilder,告诉AutoFixture跳过为collection属性赋值:

public class CollectionPropertyOmitter : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;
        if (pi != null
            && pi.PropertyType.IsGenericType
            && pi.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
            return new OmitSpecimen();

        return new NoSpecimen(request);
    }
}

Then encapsulate that in a Customization: 然后将其封装在Customization中:

public class DoNotFillCollectionProperties : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations.Add(new CollectionPropertyOmitter());
    }
}

The following tests now pass: 以下测试现在通过:

[Fact]
public void CreatePersonWithoutFillingCollectionProperty()
{
    var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
    var actual = fixture.Create<Person>();
    Assert.Null(actual.OtherClasses);
}

[Fact]
public void CreateManyStillWorks()
{
    var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
    var actual = fixture.CreateMany<Person>();
    Assert.NotEmpty(actual);
}

[Fact]
public void CreatListStillWorks()
{
    var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
    var actual = fixture.Create<List<Person>>();
    Assert.NotEmpty(actual);
}

[Fact]
public void CreateCollectionStillWorks()
{
    var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
    var actual = fixture.Create<ICollection<Person>>();
    Assert.NotEmpty(actual);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM