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

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

问题描述

我需要转换通过 table.CreateInstance()获得的Spec-flow表数据table.CreateSet()。我使用Spec flow进行数据库测试,在某些情况下,表字段值需要映射到不同的值,因为数据库表存储的是代码,而不是我们在要素文件表中输入的值。我不想在功能文件中包括代码,因为它降低了可读性。例如,如果我已如下所述输入单一状态,则我想将其映射或转换为数据传输对象/ 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, code>作为条件

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天全站免登陆