2014-09-16 4 views
5

Uso la correzione automatica e mi piacerebbe utilizzare un costruttore specifico.Seleziona costruttore specifico con AutoFixture

Ho il seguente codice e mi piace selezionare il costruttore con 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:

voglio iniettare il seguente implementazione falso. (Sto usando NSubstitute per creare l'oggetto falso.)

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

Ecco come sto cercando di usarlo.

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

Ma seleziona ancora il costruttore errato e sto ricevendo il seguente errore.

Ploeh.AutoFixture.ObjectCreationExceptionAutoFixture was unable to create an instance from Somia.Web.Routing.Template.ITemplateIterator, most likely because it has no public constructor, is an abstract or non-public type. 

Request path: 
     Somia.Web.Routing.Template.TemplateSegmentHandler --> 
     Somia.Web.Routing.Template.ITemplateIterator iterator --> 
     Somia.Web.Routing.Template.ITemplateIterator 

risposta

4

ho finito per attuare IMethodQuery e seleziono il ctor che volevo.

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 from ci in type.GetConstructors() 
       let parameter = ci.GetParameters().First() 
       where parameter.ParameterType == _type 
       select new ConstructorMethod(ci) as IMethod; 
    } 
} 

Usage:

fixture.Customize<TemplateSegmentHandler>(c => c.FromFactory(new MethodInvoker(new SelectedFirstConstructorQuery(typeof(ITemplateParameterHandler))))); 
1

Un modo possibile:

var parameterHandler = fixture.Create<ITemplateParameterHandler>(); 

var segmentHandler = fixture.Build<TemplateSegmentHandler>() 
          .FromFactory(() => new TemplateSegmentHandler(parameterHandler)); 

fixture.Inject(segmentHandler); 
+0

Sì, mi dispiace che non ho scritto nella mia OP ma voglio iniettare un oggetto personalizzato, io aggiornare il mio post con maggiori dettagli. –