Voorbeeld van het gebruik van AutoFixture met NSubstitute

Voorbeeld van het gebruik van AutoFixture met NSubstitute

In plaats van de Fixture . aan te passen instantie met de AutoNSubstituteCustomization u kunt de onderstaande aanpassing gebruiken:

var fixture = new Fixture().Customize(
    new AutoPopulatedNSubstitutePropertiesCustomization());

var result = fixture.Create<IPersonEntity>();
// -> All properties should be populated now.

De AutoPopulatedNSubstitutePropertiesCustomization is gedefinieerd als:

internal class AutoPopulatedNSubstitutePropertiesCustomization
    : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.ResidueCollectors.Add(
            new Postprocessor(
                new NSubstituteBuilder(
                    new MethodInvoker(
                        new NSubstituteMethodQuery())),
                new AutoPropertiesCommand(
                    new PropertiesOnlySpecification())));
    }

    private class PropertiesOnlySpecification : IRequestSpecification
    {
        public bool IsSatisfiedBy(object request)
        {
            return request is PropertyInfo;
        }
    }
}

Het verschil met de AutoNSubstituteCustomization is dat de bovenstaande aanpassing ook is versierd met een Postprocessor instantie om automatisch waarden in te stellen voor alle openbare eigenschappen van het gevraagde type.

Referenties :

Bovenstaande oplossing is geïnspireerd op de volgende blogartikelen van Mark Seemann:

  • Hoe AutoMoq te configureren om alle eigenschappen in te stellen
  • Hoe eigenschappen automatisch te vullen met AutoMoq

Hoewel het andere antwoord destijds als correct is gemarkeerd, wilde ik er voor de volledigheid aan toevoegen dat je inderdaad AutoNSubstituteCustomization kunt gebruiken:

var fixture = new Fixture().Customize(new AutoNSubstituteCustomization() { ConfigureMembers = true});  
var result = fixture.Create<IPersonEntity>();

Dit zal ertoe leiden dat de eigenschappen worden ingevuld.