Select specific constructor with AutoFixture

本小妞迷上赌 提交于 2019-11-30 05:43:19

问题


I'm using AutoFixture and I'd like to use a specific constructor.

I have the following code and I like to select the constructor with 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 to inject the following fake implementation. (I'm using NSubstitute to create the 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 is the way I'm trying to 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 it still selects the wrong constructor and I'm getting the following error.

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

回答1:


I ended up implementing IMethodQuery and select the ctor I 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 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)))));



回答2:


One possible way:

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

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

fixture.Inject(segmentHandler);


来源:https://stackoverflow.com/questions/25870669/select-specific-constructor-with-autofixture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!