6

我正在使用最新版本的 Autofixture,我想防止它自动填充子集合。

例如,我有一个 Person 类,它有一个 List 属性。我希望填写所有属性,但列表除外。

我尝试使用此自定义:

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

请注意,它并不总是 a Collection,但有时IList

注意2:我刚刚意识到有人还删除了自定义,IEnumerable因此为什么CreateMany()没有创建任何东西。

4

1 回答 1

9

这是一种方法。

首先实现一个 SpecimenBuilder,它告诉 AutoFixture 跳过为集合属性分配值:

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

然后将其封装在自定义中:

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);
}
于 2013-08-01T08:50:39.500 回答