Skip to content

Table to dynamic set

Marcus Hammarberg edited this page Jan 6, 2015 · 5 revisions

For tables with several rows a dynamic set can easily be created. It will produce a List for all the rows in the table.

Say that you have the following scenario:

Scenario: Create set of dynamic objects
    When I create a set of dynamic instances from this table
        | Name   | Age | Birth date | Length in meters |
        | Marcus | 39  | 1972-10-09 | 1.96             |
        | Albert | 3   | 2008-01-24 | 1.03             |
        | Gustav | 1   | 2010-03-19 | 0.84             |
        | Arvid  | 1   | 2010-03-19 | 0.85             |
    Then the 3 item should have Age equal to '1'

Then you can use this code to create a list of dynamic objects

private IList<dynamic> _set; // holds state between steps for this example

[When(@"I create a set of dynamic instances from this table")]
public void x(Table table)
{
    var set = table.CreateDynamicSet().ToList();
}

And then use the elements in the same manner as for instances created from a table (see Table to dynamic instance):

[Then(@"the (\d+) item should have Age equal to '(\d+)'")]
public void y(int itemNumber, int expectedAge)
{
    Assert.AreEqual(expectedAge, _set[itemNumber-1].Age);
}
Clone this wiki locally