Skip to content

Instantly share code, notes, and snippets.

@abenedykt
Last active January 31, 2018 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abenedykt/12d409cf3001055c80d4 to your computer and use it in GitHub Desktop.
Save abenedykt/12d409cf3001055c80d4 to your computer and use it in GitHub Desktop.
AutoNSubstitute Attrubite to create objects with nsubstitute and populate propperties
public class AutoNSubstituteAttribute : AutoDataAttribute
{
public AutoNSubstituteAttribute()
: base(()=>new Fixture()
.Customize(new AutoPopulatedNSubstitutePropertiesCustomization())
)
{
}
}
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment