Approach to generate Random specimen based on Customization
See original GitHub issueHi, 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:
- Created 6 years ago
- Comments:11 (6 by maintainers)
Top GitHub Comments
Okay, fair enough, I may have a tendency to take things too literal 😄
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 ofFoo
type and configures fixture to always return that instance. Therefore, when later you create multiple instances ofFoo
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 theCustomize<>
API as I suggested here.