Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在gherkin中实现techtalk.specflow中的数据表_C#_Datatable_Specflow_Gherkin - Fatal编程技术网

C# 如何在gherkin中实现techtalk.specflow中的数据表

C# 如何在gherkin中实现techtalk.specflow中的数据表,c#,datatable,specflow,gherkin,C#,Datatable,Specflow,Gherkin,我需要在小黄瓜中实现数据表。但它们只允许表而不允许数据表。如何在小黄瓜中实现数据表 我试过: 这是我的小黄瓜语法 Scenario: Select Even Numbers From The list Given Num List | num | | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | Then the result should even numbers only o

我需要在小黄瓜中实现数据表。但它们只允许表而不允许数据表。如何在小黄瓜中实现数据表

我试过: 这是我的小黄瓜语法

Scenario: Select Even Numbers From The list

Given Num List
| num |
| 1   | 
| 2   | 
| 3   |
| 4   |
| 5   |
| 6   |
| 7   |
| 8   |
| 9   |
| 10  |

Then the result should even numbers only on the screen.
这是生成步骤定义后的代码。这里的函数参数是Table(如何将可能的数据表替换为Table?)


表格类型是用小黄瓜表示表格的类型。
如果您直接想要一个DataTable作为函数的参数,可以使用步骤参数转换()

在您的情况下,它必须看起来像这样:

[Binding]
public class Transforms
{
    [StepArgumentTransformation]
    public DataTable TransformToDataTable(Table booksTable)
    {
        //your code to put the data from the Table to the DataTable
    }
}

用表中的数据填充DataTable仍然需要您完成。

您可以将其作为TechTalk.SpecFlow.table类的扩展方法来实现,并利用C#中的一点类反射来简化使用:

namespace YourTestProject
{
    public static class SpecFlowTableExtensions
    {
        public static DataTable ToDataTable(this Table table, params Type[] columnTypes)
        {
            DataTable dataTable = new DataTable();
            TableRow headerRow = table.Rows[0];
            int headerCellCount = headerRow.Count();

            for (int i = 0; i < headerCellCount; i++)
            {
                string columnName = headerRow[i];
                Type columnType = columnTypes[i];

                dataTable.Columns.Add(columnName, columnType);
            }

            foreach (var row in table.Rows.Skip(1))
            {
                var dataRow = dataTable.NewRow();

                for (int i = 0; i < headerCellCount; i++)
                {
                    string columnName = headerRow[i];
                    Type columnType = columnTypes[i];

                    dataRow[columnName] = Convert.ChangeType(row[i], columnType);
                }

                dataTable.AddRow(dataRow);
            }

            return dataTable;
        }

        public static DataTable ToDataTable(this Table table)
        {
            return table.ToDataTable<string>();
        }

        public static DataTable ToDataTable<TColumn0>(this Table table)
        {
            return table.ToDateTable(typeof(TColumn0));
        }

        public static DataTable ToDataTable<TColumn0, TColumn1>(this Table table)
        {
            return table.ToDateTable(typeof(TColumn0), typeof(TColumn1));
        }

        public static DataTable ToDataTable<TColumn0, TColumn1, TColumn2>(this Table table)
        {
            return table.ToDateTable(typeof(TColumn0), typeof(TColumn1), typeof(TColumn2));
        }
    }
}
以及步骤定义:

[Given(@"...")]
public void GivenNumList(Table table)
{
    DataTable dataTable = table.ToDataTable<int>();

    // dataTable.Rows[0]["num"] is an int
}
步骤定义为:

public void GivenSomeList(Table table)
{
    DataTable dataTable = table.ToDateTable<int, string>();

    // use it
}
public void GivenSomeList(表格)
{
DataTable=table.ToDateTable();
//使用它
}
按照指定SpecFlow列的顺序指定泛型类型

[Given(@"...")]
public void GivenNumList(Table table)
{
    DataTable dataTable = table.ToDataTable<int>();

    // dataTable.Rows[0]["num"] is an int
}
Given some list
    | age | name    |
    | 2   | Billy   |
    | 85  | Mildred |
public void GivenSomeList(Table table)
{
    DataTable dataTable = table.ToDateTable<int, string>();

    // use it
}