Autofixture, create a list of dictionary<string, object> with alternating key

爱⌒轻易说出口 提交于 2019-12-13 15:16:03

问题


I need a list of dictionaries, each dictionary should contain a known number of string, string pairs, the keys are deterministic but values should be a random string and each dictionary in the list must have the same keys. Some background info: the string, string pairs represents values in a database table containing product entities, I use a dictionary to add a new row to my testdatabase. To create two rows, I need two dictionaries like so:

new Dictionary<string, string>() { { "productno", "1001" }, { "productname", "testproduct" } };
new Dictionary<string, string>() { { "productno", "1002" }, { "productname", "testproduct2" } };

productno and productname are the columnnames, and the keys in the dictionaries.

I tried var dicts = new Fixture().Create<List<IDictionary<string, string>>>(); as pointed out in the comments, which gives me a list of of three dictionaries, each dictionary has a GUID as key and an a random string as value. How do I populate the keys of the dictionaries correctly, when the keys are deterministic?

My current solution is somewhat verbose, but with added benefit that it generates a random value of any type (but not tested other types than string). It only uses Autofixture to populate with random values, but would love to know if there is something built into Autofixture that does the same. What I have now:

public SqlReaderFixtureBuilder AddRows(string table, string[] columns, Type[] types, int no)
{
    var fixture = new Fixture();

    for (int rowno = 0; rowno < no; rowno++)
    {
        if (!tablerows.ContainsKey(table))
            tablerows[table] = new List<Dictionary<string, object>>();

        var values = new Dictionary<string, object>();
        for (int i = 0; i < columns.Length; i++)
        {
            values[columns[i]] = new SpecimenContext(fixture).Resolve(types[i]);
        }
        tablerows[table].Add(values);
    }
    return this;
}

calling it: AddRows("products", new[] { "productno", "productname" }, new[] { typeof(string), typeof(string) }, 30)


回答1:


It's fairly easy to create a dictionary with deterministic keys. Since the keys aren't anonymous values, it's best to create them outside of AutoFixture and merge them with values created by AutoFixture:

var fixture = new Fixture();
var columns = new[] { "productno", "productname" };
var values = fixture.Create<Generator<string>>();

var dict = columns
    .Zip(values, Tuple.Create)
    .ToDictionary(t => t.Item1, t => t.Item2);

This'll create a single Dictionary (dict) with values for both of the keys in columns.

You can package something like that in an ICustomization for Dictionary<string, string>, which will mean that when you request many Dictionary<string, string> values, you'll get multiple dictionaries all created like that.



来源:https://stackoverflow.com/questions/37511519/autofixture-create-a-list-of-dictionarystring-object-with-alternating-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!