question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add customization which appends builder to the `Customizations` collection

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Kralizekcommented, Jul 12, 2018

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.

0reactions
zvirjacommented, Jul 11, 2018

@Kralizek Consider you have the following xUnit test:

public class Issue1056
{
    [Theory, AutoData]
    public void SomeTest(SystemUnderTest sut, [Negative] int negValue)
    {
        var result = sut.GetAbs(negValue);
        Assert.Equal(-negValue, result);
    }
    
    public class SystemUnderTest
    {
        public int GetAbs(int value) => Math.Abs(value);
    }
    
    public class Negative : CustomizeAttribute
    {
        public override ICustomization GetCustomization(ParameterInfo parameter)
        {
            return new FilteringSpecimenBuilder(
                    new FixedBuilder(-10),
                    new EqualRequestSpecification(parameter)
                ).AsCustomization();
        }
    }
}

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 😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

Theme Support customizations
This customization adds settings in your theme editor to control the text position and box opacity. This customization works well if you're using...
Read more >
@Builder
One which adds a single element to the collection, and one which adds all elements of another collection to the collection. No setter...
Read more >
Custom Product Builder. Product Customizer. Product ...
Custom Product Builder combines both the functionality for product customization 'Build Your Own Product' and product personalization (Add Monograms & Artwork) ...
Read more >
ArcGIS Experience Builder: Customizing and Extending
ArcGIS Experience Builder is built on ArcGIS API 4.x for JavaScript with React. It empowers you to quickly transform your data into ...
Read more >
[GA4] Customize detail reports - Analytics Help
As an editor or administrator, you can customize a detail report to change the data shown ... Each Google Analytics property can have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found