c# – 防止Autofixture填充子集合

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

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

我尝试使用此自定义

  1. public class RemoveMultiples : ICustomization
  2. {
  3. public void Customize(IFixture fixture)
  4. {
  5. fixture.Customizations
  6. .OfType<FilteringSpecimenBuilder>()
  7. .Where(x => x.Specification is DictionarySpecification)
  8. .ToList().ForEach(c => fixture.Customizations.Remove(c));
  9. fixture.Customizations
  10. .OfType<FilteringSpecimenBuilder>()
  11. .Where(x => x.Specification is CollectionSpecification)
  12. .ToList().ForEach(c => fixture.Customizations.Remove(c));
  13. fixture.Customizations
  14. .OfType<FilteringSpecimenBuilder>()
  15. .Where(x => x.Specification is HashSetSpecification)
  16. .ToList().ForEach(c => fixture.Customizations.Remove(c));
  17. fixture.Customizations
  18. .OfType<FilteringSpecimenBuilder>()
  19. .Where(x => x.Specification is ListSpecification)
  20. .ToList().ForEach(c => fixture.Customizations.Remove(c));
  21. }
  22. }

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

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

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

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

  1. [Serializable]
  2. public class Person
  3. {
  4. private ICollection<OtherClass> _otherClasses;
  5. private string _something;
  6. public virtual ICollection<OtherClass> OtherClasses
  7. {
  8. get { return _otherClasses; }
  9. set { _otherClasses = value; }
  10. }
  11. }

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

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

解决方法

这是一种方法.

首先实现一个SpecimenBuilder,告诉AutoFixture跳过为collection属性赋值:

  1. public class CollectionPropertyOmitter : ISpecimenBuilder
  2. {
  3. public object Create(object request,ISpecimenContext context)
  4. {
  5. var pi = request as PropertyInfo;
  6. if (pi != null
  7. && pi.PropertyType.IsGenericType
  8. && pi.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
  9. return new OmitSpecimen();
  10.  
  11. return new NoSpecimen(request);
  12. }
  13. }

然后将其封装在Customization中:

  1. public class DoNotFillCollectionProperties : ICustomization
  2. {
  3. public void Customize(IFixture fixture)
  4. {
  5. fixture.Customizations.Add(new CollectionPropertyOmitter());
  6. }
  7. }

以下测试现在通过:

  1. [Fact]
  2. public void CreatePersonWithoutFillingCollectionProperty()
  3. {
  4. var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
  5. var actual = fixture.Create<Person>();
  6. Assert.Null(actual.OtherClasses);
  7. }
  8.  
  9. [Fact]
  10. public void CreateManyStillWorks()
  11. {
  12. var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
  13. var actual = fixture.CreateMany<Person>();
  14. Assert.NotEmpty(actual);
  15. }
  16.  
  17. [Fact]
  18. public void CreatListStillWorks()
  19. {
  20. var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
  21. var actual = fixture.Create<List<Person>>();
  22. Assert.NotEmpty(actual);
  23. }
  24.  
  25. [Fact]
  26. public void CreateCollectionStillWorks()
  27. {
  28. var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
  29. var actual = fixture.Create<ICollection<Person>>();
  30. Assert.NotEmpty(actual);
  31. }

猜你在找的C#相关文章