Replies: 1 comment 3 replies
-
@Kralizek there are a few ways to reduce the code depending on what exactly you consider as verbose. For example if it is accessing the customizations collection, the you could use the var fixture = new Fixture();
var categories = fixture.CreateMany<Category>(10).ToHashSet();
fixture.Customize<Category>(x => x
.FromFactory(new ElementsBuilder<Category>(categories))
.OmitAutoProperties());
var product = fixture.Create<Product>();
categories.Should().Contain(product.Category); If you're not interested in the exact set of categories then you could abstract that away using a customization (i.e. var fixture = new Fixture()
.Customize(new RandomFromFixedSequence<Category>(10));
var cateogries = fixture.CreateMany<Category>(20).ToHashSet();
var product = fixture.CreateMany<Product>(20);
cateogries.Should().HaveCount(10);
product.Should().OnlyContain(x => cateogries.Contains(x.Category)); ... or even mix the two to create a more succinct customization. public class RandomFromFixedSequence<T>(int? count) : ICustomization
{
public int? Count { get; } = count;
public void Customize(IFixture fixture)
{
var values = this.Count.HasValue
? fixture.CreateMany<T>(this.Count.Value)
: fixture.CreateMany<T>();
fixture.Customizations.Add(new ElementsBuilder<T>(values.ToHashSet()));
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A bit in the same context as #1420
Today I was helping a workmate creating a Product with a Category from a previously generated list.
I'd love to discuss how to improve the API for this scenario that seems quite common to me.
Beta Was this translation helpful? Give feedback.
All reactions