Search code examples
.netbddspecflow

List of primitives from a table in SpecFlow


I'm using SpecFlow 3 pre-release, not sure that makes a difference. Anyway I have the following table:

    | Driving Licences|
    | None            |
    | C               |
    | CE              |
    | C1              |
    | C1E             |
    | D               |
    | DE              |
    | D1              |
    | D1E             |

I want to get a list of strings. So I'm doing the following:

    [Then(@"I get the list")]
    public void CanSelectOneOrMoreFromThisList(Table table)
    {
        var values = table.CreateSet<string>();

        ...
    }

Now values is a list of 9 string but they're all empty. I could do this manually with the table rows and linq but wondered if there is something in SpecFlow to cater for this. Tried looking but only found examples for complex reference types. Thanks in advance.


Solution

  • I typically use Linq to convert the table to a list like this:

    using System.Linq;
    
    [Then(@"I get the list")]
    public void CanSelectOneOrMoreFromThisList(Table table)
    {
        IEnumerable<string> values = from row in table.Rows select row["Driving Licenses"];
    
        // ...
    }
    

    You could also write var values instead of IEnumerable<string> values.