Prevent auto-filling from filling out children's collections

I am using the latest version of Autofixture, and I would like it to not automatically populate child collections.

For example, I have a Person class that has a List property. I want all properties to be populated except the list.

I tried using this setting:

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 stops me from using .CreateMany() .

edit: I can use .CreateMany(3) and it works.

Is there any parameter that could allow me to autofill collections only when I need to?

edit2: The class person should look like this:

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

Please note that this is not always a Collection , but sometimes an IList

Note2: I just realized that someone also removed the setting for IEnumerable , so why CreateMany() does not create anything.

+6
source share
1 answer

Here is one way to do it.

Start by implementing a SpecimenBuilder that tells AutoFixture to skip assigning a value to a collection property:

 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 this in the setup:

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

Now the following tests 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); } 
+7
source

Source: https://habr.com/ru/post/950749/