c# - How to combine a Convention-based Customization with AutoFixture's [AutoData] attribute? -


i using autofixture's [autodata] attribute provide unit tests (nunit) instance of poco. example:

[test, autodata] public void create_nameisnull_throwsexception(mypoco mypoco) {..} 

i've added new string property poco must contain formed url.

naturally, [autodata] not aware of requirement , generates usual guid-based string value property. causes tests fail (because of data annotations based validation have in place).

i've followed @ploeh's advice , written convention-based customization autofixture generates formatted url string new property of mine. class called urlspecimenbuilder implements ispecimenprovider.

my question is, how can combine new ispecimenprovider [autodata]?

i don't want have go fixture.customizations.add(new urlspecimenbuilder()); in each 1 of unit test. i'm looking single test fixture setup step same thing.

you should define own version of autodataattribute , apply customizations need. if customizations relevant tests, might called defaultautodata:

[test, defaultautodata] public void create_nameisnull_throwsexception(mypoco mypoco) {..}  internal class defaultautodataattribute : autodataattribute {   public defaultautodataattribute()     : base(new fixture().customizations.add(new urlspecimenbuilder()))   {   } } 

see this mark seemann post details.


Comments