Guerrilla
Guerrilla

Reputation: 14886

NBuilder populate nested hashset

I am trying to populate some test data with NBuilder (https://github.com/nbuilder/nbuilder).

Here is my class:

public class Person
{
    public string Name { get; set; }
    public HashSet<int> AssociatedIds { get; set; }
}

I want to generate a list of Persons where they have a random list of ints with a range of 1-50. I can't figure out how to specify that NBuilder should populate the list and how I should set the constraint. Below code leaves the list null.

var people = Builder<Person>.CreateListOfSize(123).Build();

How can I populate the nested hashset with correct range?

Upvotes: 0

Views: 218

Answers (1)

Guerrilla
Guerrilla

Reputation: 14886

Figured out a way of doing it:

        var people = Builder<Person>.CreateListOfSize(123)
            .All()
                .With(a => a.AssociatedIds = 
                    Enumerable.Range(0, 50)
                        .Select(x => new Tuple<int,int>(new Random().Next(1,1000), x))
                        .OrderBy(x => x.Item1)
                        .Take(new Random().Next(1,50))
                        .Select(x => x.Item2)
                        .ToHashSet())
            .Build();

Upvotes: 0

Related Questions