自动定位,创建字典的列表< string,object>用交替键 [英] Autofixture, create a list of dictionary<string, object> with alternating key

查看:79
本文介绍了自动定位,创建字典的列表< string,object>用交替键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个字典列表,每个字典应包含已知数量的字符串,字符串对,键是确定性的,但值应为随机字符串,并且列表中的每个字典必须具有相同的键. 一些背景信息:字符串,字符串对表示包含产品实体的数据库表中的值,我使用字典向testdatabase添加新行.要创建两行,我需要像这样的两个字典:

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和productname是列名,以及字典中的键.

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

我按注释中的说明尝试了var dicts = new Fixture().Create<List<IDictionary<string, string>>>();,这给了我三个字典的列表,每个字典都有一个GUID作为键,一个随机字符串作为值. 当键是确定性的时,如何正确填充字典的键?

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?

我当前的解决方案有些冗长,但是它还具有生成任何类型的随机值(但未测试字符串以外的其他类型)的优点. 它仅使用Autofixture填充随机值,但很想知道Autofixture内置的功能是否与之相同.我现在所拥有的:

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;
}

调用它:AddRows("products", new[] { "productno", "productname" }, new[] { typeof(string), typeof(string) }, 30)

推荐答案

使用确定性键创建字典非常容易.由于密钥不是匿名值,因此最好在AutoFixture之外创建它们,并将它们与AutoFixture创建的值合并:

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);

这将创建一个单独的词典(dict),其中包含columns中两个键的值.

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

您可以在Dictionary<string, string>ICustomization中打包类似的内容,这意味着当您请求多个Dictionary<string, string>值时,您将获得多个创建的字典.

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.

这篇关于自动定位,创建字典的列表&lt; string,object&gt;用交替键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆