Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate dictionary with AutoFixture

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.

like image 377
John Avatar asked Jul 19 '14 18:07

John


1 Answers

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.

like image 84
Adam C Avatar answered Sep 28 '22 10:09

Adam C