Search code examples
c#bddspecflowgherkin

Is it possible to automatically detect types in Specflow tables?


If I have a Techtalk.Specflow.Table, is it possible to detect automatically the appropriate types of the elements in the table ?

For exemple, if I have the following steps :

Given the following ticket sold on the 2019-01-01
|TicketId|Owner     |Amount|Seat|
|0033    |John Doe  |20.00 |3F  |

If I define my step like this

[Given(@"Given the following ticket sold on the (.*)")]
public void GivenTheFollowingPosition(DateTime date, Table table)
{
}

Specflow is capable of detecting and cast the date correctly. Therefore I assume it should also be capable of doing so for the elements of the table. Do you know if it is possible to achieve it and how ?

Have a nice day


Solution

  • Have a look at SpecFlow Assist Helpers. There are a few helpful methods, you can try to use table.CreateInstance<T> method to convert row in your table to object for future use. You can also specify the custom mapping using TableAliases attribute, see Working Effectively with SpecFlow Tables article for details