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.

Create method of Custom ISpecimenBuilder is not called when ISpecimenBuilder.CreateMany(int count) overload is used

See original GitHub issue

Hi, this is related to: https://github.com/AutoFixture/AutoFixture/issues/960.

I’ve implemented a custom ISpecimenBuilder to generate distinct GUIDs with a given lenght:

public class UniqueShortGuidBuilder<TEntity> : ISpecimenBuilder
{
    private readonly string propName;
    private readonly int lenght;

    public UniqueShortGuidBuilder(Expression<Func<TEntity, string>> expr, int lenght)
    {
        propName = ((MemberExpression)expr.Body).Member.Name;
        this.lenght = lenght;
    }

    public object Create(object request, ISpecimenContext context)
    {
       // Omitted for brevity
    }

In my tests:

Fixture.Customizations.Add(new UniqueShortGuidBuilder<Foo>(x => x.Identifier, 6));
var fooList1 = Fixture.CreateMany<Foo>().ToList(); // This works (calls UniqueShortGuidBuilder.Create)
var fooList2 = Fixture.CreateMany<Foo>(10).ToList(); // This does not

Is this by design? Is there a workaround?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thiagomajeskcommented, Jan 12, 2018

@zvirja Thanks a lot for the detailed explanation!

Yep, for now that is so. However I have strong plans to create a doc site in the nearby future, so all the API will be described with samples.

That will be really really helpfull 😄.

1reaction
zvirjacommented, Jan 12, 2018

What did you mean by “more concise”? What changes exactly?

There is no difference between the following two snippets:

public class FooCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        var specimen = fixture.Build<Foo>()
            .OmitAutoProperties()
            .With(x => x.Identifier)
            .Create();
        
        fixture.Register(() => specimen);
    }
}

and

public class FooCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Freeze<Empresa>(c => c
            .OmitAutoProperties()
            .With(x => x.Identifier)
        );
    }
}

Basically, both them do exactly the same - create a customized instance of the Empresa object and configure fixture to always return that object when instance of Empresa is requested. Notice, with such configuration fixture will always return the same object without creation of new ones. That’s why the method is called Freeze - it freezes the object.

If you don’t need that “freezing” behavior and would like to get a new object for each time, you can modify customization as following:

public class FooCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customize<Empresa>(c => c
            .OmitAutoProperties()
            .With(x => x.Identifier)
        );
    }
}

In this case you still configure how exactly to construct object, however don’t pin the particular instance, so a new one is created for each time.

Both the options are perfectly fine and you should evaluate your scenario to find which exact one you need.

… rely more on SO and @ploeh’s blog for documentation …

Yep, for now that is so. However I have strong plans to create a doc site in the nearby future, so all the API will be described with samples.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Approach to generate Random specimen based on ...
I want to be able to generate distinct values based on a ICustomization using ISpecimenBuilder.CreateMany . I was wondering what would be the ......
Read more >
Class SpecimenFactory - AutoFixture
Creates anonymous variables from ISpecimenContext or ISpecimenBuilder instances. ... CreateMany<T>(this IPostprocessComposer<T> composer, int count) ...
Read more >
Create and Build
This overload is used to provide a lazily-evaluated factory method used to generate the value of the property. The factory method can have...
Read more >
AutoFixture Generate Specific Format with Specimen Builders
Next up is the Create method. This is the only method you are required to implement when implementing ISpecimenBuilder . It expects a...
Read more >
Convention-based Customizations with AutoFixture - ploeh blog
In a nutshell, a custom ISpecimenBuilder can be used to implement all sorts of custom conventions for AutoFixture. The one shown here applies ......
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