Generate dictionary with AutoFixture
Asked Answered
B

1

18

For a list, we can do

fixture.CreateMany<List<string>>(1000); // with 1000 elements

but how to do it with a dictionary? And to be able to specify the number of elements to be generated.

Bruton answered 19/7, 2014 at 18:6 Comment(0)
H
31

You could simply create the items then build the dictionary, like this:

fixture
  .CreateMany<KeyValuePair<int, string>>(1000)
  .ToDictionary(x => x.Key, x => x.Value);

This is more-or-less what AutoFixture does internally.

Another alternative would be to create a new ICustomization, which intercepts requests for any Dictionary<,> and builds them. It could be implemented using code from existing classes.

Hosbein answered 20/7, 2014 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.