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.

Approach to generate Random specimen based on Customization

See original GitHub issue

Hi, I want to be able to generate distinct values based on a ICustomization using ISpecimenBuilder.CreateMany. I was wondering what would be the best solution since AutoFixture will generate the same values for all entities.

public class FooCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        var specimen = fixture.Build<Foo>()
            .OmitAutoProperties()
            .With(x => x.CreationDate, DateTime.Now)
            .With(x => x.Identifier, Guid.NewGuid().ToString().Substring(0, 6)) // Should gen distinct values
            .With(x => x.Mail, $"contactme@mail.com")
            .Create();
            
            fixture.Register(() => specimen);
    }
}

PS.: The main goal is to reduce the amount of code on my tests, so I must avoid calling With() to customize the values for every test. Is there a proper way of doing this?

Update: I just read this response from @ploeh: https://github.com/AutoFixture/AutoFixture/issues/361#issuecomment-86873060 that might be what I’m looking for.
This approach has many drawbacks: First it seems really counter intuitive to call Create<List<Foo>>() because it kinda of defeats the purpose of what to expect from CreateMany<Foo>; that would generate a hardcoded sized List<List<Foo>> (?). Another drawback is that I’d have to have two customizations for each entity; one to create custom collections and another one to create a single instance, since we are overriding the behaviour of Create<T> to create a collections.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ploehcommented, Jan 15, 2018

Okay, fair enough, I may have a tendency to take things too literal 😄

1reaction
zvirjacommented, Jan 12, 2018

If I remove the Fixture.Customize(new FooCustomization()); the test will pass, otherwise not.

Well, in fact that is the expected behavior if you review the situation more carefully 😅 Please see my reply in #962. The provided implementation of the FooCustomization creates instance of Foo type and configures fixture to always return that instance. Therefore, when later you create multiple instances of Foo type, each time the same object is returned and your builder is not further invoked.

If you want a new instance to be created for each time you request Foo, use the Customize<> API as I suggested here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Approach to generate Random specimen based on ...
I was wondering what would be the best solution since AutoFixture will generate the same values for all entities. public class FooCustomization ......
Read more >
Generating random samples from a custom distribution
I am trying to generate random samples from a custom pdf using R. My pdf is: fX(x)=32(1−x2),0≤x≤1. I generated uniform samples and then ......
Read more >
Generate Random Parameter Values - MATLAB & Simulink
Generate Custom Parameter Values at the Command Line​​ This example shows how to generate random parameter values with a custom distribution when performing ......
Read more >
How to select random sample in Excel
Then I would advise you to opt for 'Random Generator' from the Ablebits 'Randomize' drop-down menu and make a 'Custom list' consisting of...
Read more >
Generating Random Numbers From a Specific Distribution ...
To make a candidate sample, take a small random step from the current x point to get a new x, either in the...
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