Search code examples
c#dictionaryautofixture

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.


Solution

  • 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.