How to convert SpecFlow table to array of strings

Viewed 7566

I am generating content for a text file and trying to test the output with a SpecFlow table. My Then statement looks like this:

Then the content should be 
| Line           |
| This is Line 1 |
| This is Line 2 |
| etc...         |

I'm turning this into an array of strings in the Step file like this:

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

This will give me an array of strings with 3 elements, ignoring the first "Line" as the table header. But it feels a bit awkward. Is there a better way to turn this into an array of string? Bonus points if it also can convert into arrays immutable types like int, etc.

1 Answers

you could write your own extension

public static class MyTableExtenstions
    {
        public static string[] AsStrings(this Table table, string column)
        {
            return table.Rows.Select(row => row[column]).ToArray();
        }
    }

then

string[] expectedLines = table.AsStrings("Line");
Related