Rasika Rasika - 1 year ago 82
C# Question

How to transform Spec-flow table data into different values

I need to transform Spec-flow table data that we get via

. 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 |

Answer Source

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 condition

 public static IEnumerable<T> CreateSetWithValueTransfer<T>(this Table table)
        var mapper = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
            {"Value1", "Code1"},
            {"value2", "Code2"}

        var set = ChangeValues(table, mapper);
        return set.CreateSet<T>();

    private static Table ChangeValues(Table table, Dictionary<string, string> mapper)
        var mappedTable = new Table(table.Header.ToArray());
        foreach (var row in table.Rows)
            mappedTable.AddRow(row.Values.Select(x => mapper.ContainsKey(x) ? mapper[x] : x).ToArray());
        return mappedTable;