c# - Preventing Autofixture from filling child collections -


i'm using latest version of autofixture, , i'd prevent filling automatically child collections.

for example, have class person has list property. want properties filled, except list.

i tried using customization :

public class removemultiples : icustomization {     public void customize(ifixture fixture)     {         fixture.customizations             .oftype<filteringspecimenbuilder>()             .where(x => x.specification dictionaryspecification)             .tolist().foreach(c => fixture.customizations.remove(c));         fixture.customizations             .oftype<filteringspecimenbuilder>()             .where(x => x.specification collectionspecification)             .tolist().foreach(c => fixture.customizations.remove(c));         fixture.customizations             .oftype<filteringspecimenbuilder>()             .where(x => x.specification hashsetspecification)             .tolist().foreach(c => fixture.customizations.remove(c));         fixture.customizations             .oftype<filteringspecimenbuilder>()             .where(x => x.specification listspecification)             .tolist().foreach(c => fixture.customizations.remove(c));     } } 

but prevents me using .createmany().

edit: can use .createmany(3) , works.

is there setting somewhere let me autofill collections when need to?

edit2: class person should this:

[serializable] public class person {     private icollection<otherclass> _otherclasses;      private string _something;     public virtual icollection<otherclass> otherclasses     {         { return _otherclasses; }         set { _otherclasses = value; }     } } 

note it's not collection, ilist

note2: realized removed customization ienumerable hence why createmany() doesn't create anything.

here's 1 way it.

start implementing specimenbuilder tells autofixture skip assigning value collection property:

public class collectionpropertyomitter : ispecimenbuilder {     public object create(object request, ispecimencontext context)     {         var pi = request propertyinfo;         if (pi != null             && pi.propertytype.isgenerictype             && pi.propertytype.getgenerictypedefinition() == typeof(icollection<>))             return new omitspecimen();          return new nospecimen(request);     } } 

then encapsulate in customization:

public class donotfillcollectionproperties : icustomization {     public void customize(ifixture fixture)     {         fixture.customizations.add(new collectionpropertyomitter());     } } 

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

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -