-
Notifications
You must be signed in to change notification settings - Fork 27
Table to dynamic instance
Marcus Hammarberg edited this page Jan 6, 2015
·
2 revisions
You can easily create dynamic instances from a table. Say that you have the following scenario:
Scenario: Create a dynamic instance from a table
When I create a dynamic instance from this table
| Name | Age | Birth date | Length in meters |
| Marcus | 39 | 1972-10-09 | 1.96 |
Then the Name property should equal 'Marcus'
Then you can create a dynamic instance with this simple construct:
private dynamic _instance;
[When(@"I create a dynamic instance from this table")]
public void CreateDynamicInstanceFromTable(Table table)
{
_instance = table.CreateDynamicInstance();
}
And start to use the properties like this:
[Then(@"the Name property should equal '(.*)'")]
public void NameShouldBe(string expectedValue)
{
Assert.AreEqual((string)_instance.Name, expectedValue);
}
A dynamic instance can also be created from a vertical table, in field-value style tables like this:
Scenario: Create dynamic instance from table with Field and Values
When I create a dynamic instance from this table
| Field | Value |
| Name | Marcus |
| Age | 39 |
| Birth date | 1972-10-09 |
| Length in meters | 1.96 |
Then the Name property should equal 'Marcus'
Note that the column names, in the table above, doesn't matter, the first one will be used as field names, the second for values.
Then you can again create a dynamic instance like this (same as above):
private dynamic _instance;
[When(@"I create a dynamic instance from this table")]
public void CreateDynamicInstanceFromTable(Table table)
{
_instance = table.CreateDynamicInstance();
}
And start to use the properties like this:
[Then(@"the Name property should equal '(.*)'")]
public void NameShouldBe(string expectedValue)
{
Assert.AreEqual((string)_instance.Name, expectedValue);
}