I need to transform Spec-flow table data that we get via table.CreateInstance()
or table.CreateSet()
. I am using Spec flow for DB testing and in some cases, Table field values needs to be mapped to different values as DB tables are storing codes instead of the the values we have entered in tables of feature files. I do not want to include the codes in feature files as it reduces the readability. For example, If I have entered Single for status as mentioned below, I want it to be mapped or transform to S in the data transfer object / POCO. What is the best approach ? Thanks in advance.
Given I entered the following data into the new account form:
| Name | Birthdate | Status |
| John Butcher| 2/2/1902 | Single |
I posed a similar question with a different example here. Seems like it should be possible to apply StepArgumentTransforms to CreateInstance/CreateSet, but just hasn't been implemented yet for basic types that already get transformed in the table conversion.
In your case (unlike mine), I think you could do it relatively easily with a custom ValueRetriever.
As Sam pointed out, we can use StepArgumentTransformarion or an approach similar to below. Add if else inside the extension methods if you want to map Value1 to Code1 for one type and Value1 to CodeX in another type using
typeof(T).Name.Equals(typeof(yourtype).Name)
as the conditionI've started using "Test Models," which ended up being view models for my spec flow tests.
Using your example:
The Domain Model:
And the "Test Model":
The step defintion:
For this one particular example it might be more than you need, but I've found this pattern works well when the data in the Scenario needs to be in a human readable format, which gets translated to a complex format in your Domain Model.
I'm not aware of anyway to do this automatically, so I can only think of two possibilities.
CreateInstance
orCreateSet
methods and instead do all of the mapping manually, but encapsulate it in a[StepArgumentTransformation]