如何将 SpecFlow table 转换为字符串数组
How to convert SpecFlow table to array of strings
我正在为文本文件生成内容并尝试使用 SpecFlow table 测试输出。我的 Then
语句如下所示:
Then the content should be
| Line |
| This is Line 1 |
| This is Line 2 |
| etc... |
我将其转换为 Step 文件中的字符串数组,如下所示:
[Then(@"the content should be")]
public void ThenTheContentShouldBe(Table table)
{
string[] expectedLines = table.Rows.Select(x => x.Values.FirstOrDefault()).ToArray();
...
}
这将给我一个包含 3 个元素的字符串数组,忽略第一个 "Line" 作为 table header。但是感觉有点尴尬。有没有更好的方法把它变成一个 string
的数组?如果它也可以转换成数组 immutable 类型如 int
等
加分
您可以编写自己的扩展程序
public static class MyTableExtenstions
{
public static string[] AsStrings(this Table table, string column)
{
return table.Rows.Select(row => row[column]).ToArray();
}
}
然后
string[] expectedLines = table.AsStrings("Line");
我正在为文本文件生成内容并尝试使用 SpecFlow table 测试输出。我的 Then
语句如下所示:
Then the content should be
| Line |
| This is Line 1 |
| This is Line 2 |
| etc... |
我将其转换为 Step 文件中的字符串数组,如下所示:
[Then(@"the content should be")]
public void ThenTheContentShouldBe(Table table)
{
string[] expectedLines = table.Rows.Select(x => x.Values.FirstOrDefault()).ToArray();
...
}
这将给我一个包含 3 个元素的字符串数组,忽略第一个 "Line" 作为 table header。但是感觉有点尴尬。有没有更好的方法把它变成一个 string
的数组?如果它也可以转换成数组 immutable 类型如 int
等
您可以编写自己的扩展程序
public static class MyTableExtenstions
{
public static string[] AsStrings(this Table table, string column)
{
return table.Rows.Select(row => row[column]).ToArray();
}
}
然后
string[] expectedLines = table.AsStrings("Line");