[英]How to convert a SpecFlow table into a Dictionary<string, List<string>> c#
I have to following SpecFlow code: 我必须遵循以下SpecFlow代码:
And I get and validate parameters
| ParameterName| Value | Answers | Mandatory | Meta | Modified | ReadOnly | Submit | SubmitValues | Tag |
| SurName | | | true | | false | false | true | | input |
| Name | | | true | | false | false | false | | input |
.....
And i want this table to convert it into a Dictionary<string, List<string>>
, the head columns will be the keys and the rest of the information will be the values. 我希望此表将其转换为
Dictionary<string, List<string>>
,头列将是键,其余信息将是值。 I hardcoded some values: 我对一些值进行了硬编码:
Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
dictionary.Add("ParameterName", new List<string> {"SurName", "Name", "Age"});
dictionary.Add("Value", new List<string> { "", "", "" });
dictionary.Add("Answers", new List<string> { "", "", "" });
dictionary.Add("Mandatory", new List<string> { "true", "true", "true" });
dictionary.Add("Meta", new List<string> { "", "", "" });
dictionary.Add("Modified", new List<string> { "false", "false", "false" });
dictionary.Add("ReadOnly", new List<string> { "false", "false", "false" });
dictionary.Add("Submit", new List<string> { "true", "false", "true" });
dictionary.Add("SubmitValues", new List<string> { "", "", "" });
dictionary.Add("Tag", new List<string> { "input", "input", "select" });
But the actual table has a lot of values and i need to do this for all of them, and they might change, that's why i don't need a hardcoded dictionary. 但是实际表中有很多值,我需要对所有值进行此操作,并且它们可能会更改,这就是为什么我不需要硬编码字典的原因。 How can I do this?
我怎样才能做到这一点?
In such a dictionary the retrieval of the same row is quite awkward (you should index the Value lists)... 在这样的字典中,同一行的检索非常麻烦(您应该为“值”列表建立索引)...
Instead, I would create a List<TestParameters>
where TestParameters
class contains a row with normal strongly typed properties: 相反,我将创建一个
List<TestParameters>
,其中TestParameters
类包含具有常规强类型属性的行:
public class TestParameters
{
public string ParameterName { set; set; }
public int Value { set; set; }
public bool Mandatory { set; set; }
// etc.
}
So now you have somewhere a test step like this: 因此,现在您可以在某个测试步骤中进行如下操作:
[Given(@"I get and validate parameters")]
public void GetParameters(Table parameters)
{
}
Simply replace the Table
with your more specific type: 只需将
Table
替换为您更具体的类型:
[Given(@"I get and validate parameters")]
public void GetParameters(List<TestParameters> parameters)
{
}
And just define a Table->List transformation step in a helper class: 只需在助手类中定义Table-> List转换步骤:
[Binding]
public class Transformations
{
[StepArgumentTransformation]
public List<TestParameters> GetTestParameters(Table table)
{
return table.Rows.Select(row => new TestParameters
{
// string prop
ParameterName = row["ParameterName"],
// int prop
Value = !String.IsNullOrEmpty(row["Value"]) ? Int32.Parse(row["Value"]) : 0,
// bool prop
Mandatory = row["Mandatory"]?.ToLowerInvariant() == "true"
// TODO: other properties
}).ToList();
}
}
Of course, the result of the transformation can be Dictionary<string, List<string>>
, too, if you really insist to that... 当然,如果您真的坚持的话
Dictionary<string, List<string>>
转换的结果也可以是Dictionary<string, List<string>>
。
You could also use the CreateSet extension method from the TechTalk.SpecFlow.Assist namespace. 您还可以使用TechTalk.SpecFlow.Assist命名空间中的CreateSet扩展方法。
Have a look at the documentation here: http://specflow.org/documentation/SpecFlow-Assist-Helpers/ 在这里查看文档: http : //specflow.org/documentation/SpecFlow-Assist-Helpers/
Small example: 小例子:
CreateSet is an extension method off of Table that will convert the table data to a set of objects.
CreateSet是Table的扩展方法,它将表数据转换为一组对象。 For example, if you have the following step:
例如,如果您具有以下步骤:
Given these products exist
| Sku | Name | Price |
| BOOK1 | Atlas Shrugged | 25.04 |
| BOOK2 | The Fountainhead | 20.15 |
You can convert the data in the table to a set of objects like so:
您可以将表中的数据转换为一组对象,如下所示:
[Given(@"Given these products exist")]
public void x(Table table)
{
var products = table.CreateSet<Product>();
// ...
}
It's too late to answer this question but probably might be helpful for others. 回答这个问题为时已晚,但可能会对其他人有所帮助。
The better way to handle such data in collection should be in format List>. 处理集合中此类数据的更好方法应采用List>格式。
Following extension method should help in this regard- 以下扩展方法应在这方面有所帮助-
public static List<Dictionary<string, string>> ConvertToDictionaryList(this Table dt)
{
var lstDict = new List<Dictionary<string, string>>();
if (dt!=null)
{
var headers = dt.Header;
foreach (var row in dt.Rows)
{
var dict = new Dictionary<string, string>();
foreach (var header in headers)
{
dict.Add(header, row[header]);
}
lstDict.Add(dict);
}
}
return lstDict;
}
But if you still want to use Dictionary>, then you can use following snippet 但是,如果您仍然想使用Dictionary>,则可以使用以下代码段
public static List<Dictionary<string, string>> ConvertToDictionaryList(this Table dt)
{
var lstDict = new Dictionary<string, List<string>>();
if (dt != null)
{
var headers = dt.Header;
foreach (var row in dt.Rows)
{
foreach (var header in headers)
{
if (lstDict.ContainsKey(header))
{
lstDict[header].Add(row[header]);
}
else
{
lstDict.Add(header, new List<string> { row[header] });
}
}
}
}
return lstDict;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.