Example of how to use AutoFixture with NSubstitute
Though the other answer has been marked as correct back then, I just wanted to add for completeness that you can indeed use AutoNSubstituteCustomization:
var fixture = new Fixture().Customize(new AutoNSubstituteCustomization() { ConfigureMembers = true});
var result = fixture.Create<IPersonEntity>();
This will result in the properties being populated.
Instead of customizing the Fixture
instance with the AutoNSubstituteCustomization
you may use the customization below:
var fixture = new Fixture().Customize(
new AutoPopulatedNSubstitutePropertiesCustomization());
var result = fixture.Create<IPersonEntity>();
// -> All properties should be populated now.
The AutoPopulatedNSubstitutePropertiesCustomization
is defined as:
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;
}
}
}
The difference with the AutoNSubstituteCustomization
is that the above customization is also decorated with a Postprocessor
instance to automatically set values for all the public properties of the requested type.
References:
The above solution is inspired by the following blog articles by Mark Seemann:
- How to configure AutoMoq to set up all properties
- How to automatically populate properties with AutoMoq