如何将SpecFlow表转换为字符串数组

djs*_*djs 2 c# specflow

我正在为文本文件生成内容并尝试使用SpecFlow表测试输出.我的Then陈述如下:

Then the content should be 
| Line           |
| This is Line 1 |
| This is Line 2 |
| etc...         |
Run Code Online (Sandbox Code Playgroud)

我将这变成Step文件中的一个字符串数组,如下所示:

[Then(@"the content should be")]
public void ThenTheContentShouldBe(Table table)
{
    string[] expectedLines = table.Rows.Select(x => x.Values.FirstOrDefault()).ToArray();
    ...
}
Run Code Online (Sandbox Code Playgroud)

这将给我一个包含3个元素的字符串数组,忽略第一个"Line"作为表头.但感觉有点尴尬.有没有更好的方法将其变成一个数组string?奖励点如果它也可以转换成数组不可变类型,如int等.

Kei*_*las 5

你可以写自己的扩展

public static class MyTableExtenstions
    {
        public static string[] AsStrings(this Table t, string column)
        {
            return t.Rows.Select(r => r[column]).ToArray();
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后

string[] expectedLines = table.AsStrings("Line");
Run Code Online (Sandbox Code Playgroud)

  • 由于非常懒,我只是做了 `var results = table.Rows.Select(r => r[0]).ToArray()` (2认同)