Add customization which appends builder to the `Customizations` collection
See original GitHub issueWhen you would like to build CustomizeAttribute
instance (extension point for NUnit and xUnit), you should return the ICustomization
instance:
public class CustomAttribute : CustomizeAttribute
{
public override ICustomization GetCustomization(ParameterInfo parameter)
{
// Here I should return ICustomization
}
}
I found that often all I need to do is to insert ISpecimenBuilder
to the fixture.Customizations
collection. However, to do that I need to create an extra class, implementing the ICustomization
interface and adding my builder to the collection. It makes implementation polluted and it’s hard to focus on the logic.
I suggest to add this type to the AutoFixture library, so we could write simpler customizations:
public class FortyTwo : CustomizeAttribute
{
public override ICustomization GetCustomization(ParameterInfo parameter)
{
return new AddCustomBuilderCustomization(
new FilteringSpecimenBuilder(
new FixedBuilder(42),
new EqualRequestSpecification(parameter)
));
}
}
The only thing I’m unsure is the customization name. Usually we follow the convention to suffix customizations with Customization
word, but here it might add noise. I see following candidates:
AddCustomBuilderCustomization
AddBuilderToCustomizations
AddBuilderToCustomizationsCustomization
CustomizationAppender
CustomizationAppenderCustomization
None of the name looks great 😞 Also I don’t like the “append” part, as later we might want to specify the position (first vs last), so append will be confusing.
@moodmosaic Do you see a nice name for this customization type?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
Oh nice! Thank you for your explanation! I already see some “arrange” code that can be moved away from the unit test body!
The more I learn about AutoFixture, the more I love it.
@Kralizek Consider you have the following xUnit test:
You want to customize the test input value, so that fixture provides you with a negative number (by default the positive numbers are returned). To do that you can write your own customization attribute. As a part of the customization attribute implementation you should return the
ICustomization
instance. Here is where the extension method is very handy, as otherwise you need to create an extra class.Sure, the real-world implementation should be more clever, but I just wanted to demonstrate you the idea 😅