I am using AutoFixture and I would like to use a specific constructor.
I have the following code, and I like to select a constructor using 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 introduce the following fake implementation. (I use NSubstitute to create a 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; } }
This is how I try 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 he still chooses the wrong constructor, and I get 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
source share