如何将 Spec-flow 表数据转换为不同的值 [英] How to transform Spec-flow table data into different values

查看:33
本文介绍了如何将 Spec-flow 表数据转换为不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转换通过 table.CreateInstance()table.CreateSet() 获得的 Spec-flow 表数据.我正在使用 Spec 流进行 DB 测试,在某些情况下,表字段值需要映射到不同的值,因为 DB 表存储的是代码,而不是我们在特征文件表中输入的值.我不想将代码包含在功能文件中,因为它会降低可读性.例如,如果我输入了 Single 作为下面提到的状态,我希望它在数据传输对象/POCO 中映射或转换为 S.最好的方法是什么?提前致谢.

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      |

推荐答案

正如 Sam 指出的,我们可以使用 StepArgumentTransformarion 或类似下面的方法.如果要使用 typeof(T).Name.Equals(typeof(yourtype).Name) 将一种类型的 Value1 映射到 Code1 并将 Value1 映射到另一种类型的 CodeX ,请在扩展方法中添加 if else条件

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;
    }

这篇关于如何将 Spec-flow 表数据转换为不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆