Create method of Custom ISpecimenBuilder is not called when ISpecimenBuilder.CreateMany(int count) overload is used
See original GitHub issueHi, 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:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@zvirja Thanks a lot for the detailed explanation!
That will be really really helpfull 😄.
There is no difference between the following two snippets:
and
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 ofEmpresa
is requested. Notice, with such configuration fixture will always return the same object without creation of new ones. That’s why the method is calledFreeze
- 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:
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.
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.