Select specific constructor with AutoFixture -


i'm using autofixture , i'd use specific constructor.

i have following code , select constructor itemplateparameterhandler.

public sealed class templatesegmenthandler : itemplatesegmenthandler {     public templatesegmenthandler(itemplateiterator iterator)         : this(new templateparameterhandler(iterator))     {         contract.requires(iterator != null);     }      public templatesegmenthandler(itemplateparameterhandler parameterhandler)     {         contract.requires(parameterhandler != null);          _parameterhandler = parameterhandler;          _parameterhandler.ending += endingparameter;     }      // ... } 

edit:

i want inject following fake implementation. (i'm using nsubstitute create fake object.)

public sealed class customtemplateparameter : icustomization {     private readonly itemplateparameterhandler _context;      public customtemplateparameter()     {         _context = substitute.for<itemplateparameterhandler>();     }      public void customize(ifixture fixture)     {         fixture.customize<itemplateparameterhandler>(c => c.fromfactory(() => _context));     }      public customtemplateparameter setcatchall(bool iscatchall)     {         _context.iscatchall.returns(iscatchall);          return this;     } } 

here way i'm trying use it.

[fact] public void should_return_true_when_the_segment_has_a_catch_all_parameter() {     templatesegmenthandler segmenthandler = new fixture().customize(new templatesegmenthandlerfixture())                                                          .customize(new customtemplateparameterhandler()                                                                         .setcatchall(true))                                                          .create<templatesegmenthandler>();      segmenthandler.parameter.start();     segmenthandler.parameter.end();      segmenthandler.hascatchall.should().betrue(); } 

but still selects wrong constructor , i'm getting following error.

ploeh.autofixture.objectcreationexceptionautofixture unable create instance somia.web.routing.template.itemplateiterator, because has no public constructor, abstract or non-public type.  request path:       somia.web.routing.template.templatesegmenthandler -->         somia.web.routing.template.itemplateiterator iterator -->          somia.web.routing.template.itemplateiterator 

i ended implementing imethodquery , select ctor wanted.

public sealed class selectedfirstconstructorquery : imethodquery {     private readonly type _type;      public selectedfirstconstructorquery(type type)     {         _type = type;     }      public ienumerable<imethod> selectmethods(type type)     {         if (type == null)         {             throw new argumentnullexception("type");         }          return ci in type.getconstructors()                let parameter = ci.getparameters().first()                parameter.parametertype == _type                select new constructormethod(ci) imethod;     } } 

usage:

fixture.customize<templatesegmenthandler>(c => c.fromfactory(new methodinvoker(new selectedfirstconstructorquery(typeof(itemplateparameterhandler))))); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -