c# – 防止Autofixture填充子集合

前端之家收集整理的这篇文章主要介绍了c# – 防止Autofixture填充子集合前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用最新版本的Autofixture,我想阻止它自动填充子集合.

例如,我有一个具有List属性的Person类.我想要填写所有属性,列表除外.

我尝试使用此自定义

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));
    }
}

但它也阻止我使用.CreateMany().

编辑:我可以使用.CreateMany(3),它的工作原理.

是否有某个设置可以让我只在我需要时自动填充集合?

edit2:班级人员应如下所示:

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

请注意,它并不总是一个集合,但有时是IList

注意2:我刚刚意识到有人也删除了IEnumerable的Customization,因此CreateMany()为什么不创建任何东西.

解决方法

这是一种方法.

首先实现一个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);
    }
}

然后将其封装在Customization中:

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

以下测试现在通过:

[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);
}
原文链接:https://www.f2er.com/csharp/243771.html

猜你在找的C#相关文章