如何在SpecFlow中处理没有标题的表?

时间:2022-10-04 09:22:16

Coming from using Gherkin in Ruby, I want to see if SpecFlow on C# .NET is an option for me. In Ruby I was able to process a table that has the fields in the first column and the values in the second, like this:

来自在Ruby中使用Gherkin,我想看看C#.NET上的SpecFlow是否适合我。在Ruby中,我能够处理一个表,其中包含第一列中的字段和第二列中的值,如下所示:

Then I see a summary of details in the overview page
| Service           | Taxi        |
| Arrival Station   | Middlebourg |
| Arrival Vehicle   | SAT12901    |
| Arrival Date      | 27/06/2014  |
| No of Passengers  | 2           |

Within SpecFlow I can use the TechTalk.SpeclFlow.Table object which has these functions:

在SpecFlow中,我可以使用具有以下功能的TechTalk.SpeclFlow.Table对象:

  • CreateInstance : Assumes that "Service" and "Taxi" are headings which they're not.
  • CreateInstance:假设“服务”和“出租车”是他们不是的标题。

  • CreateSet : Also ignores first line and returns key/value pairs. Also not working.
  • CreateSet:同时忽略第一行并返回键/值对。也行不通。

Of course a work around here is to change the layout of my table to match the working of SpecFlow's table object. But is there an alternative, like the "rows_hash" function in Ruby? Thanks!

当然,这里的工作是更改我的表的布局以匹配SpecFlow的表对象的工作。但是有没有替代方案,比如Ruby中的“rows_hash”函数?谢谢!

1 个解决方案

#1


0  

It seems that SpecFlow always assumes a header. You can access this header with:

似乎SpecFlow总是采用标题。您可以访问此标头:

table.Header

to get raw access to the headings. This is just a collection of strings so you can access the individual elements with:

获得标题的原始访问权限。这只是一个字符串集合,因此您可以使用以下命令访问各个元素:

var firstRowFirstColumn = table.Header[0];
var firstRowSecondColumn = table.Header[1]; 

You can then just access the remaining individual rows in the table with something like this:

然后,您可以使用以下内容访问表中的其余各行:

foreach(var row in table.Rows)
{
   var first = row[0];
   var second = row[1];
   ... use the values ...
}

to get all of the other values in the table

获取表中的所有其他值

if you want raw access without the headers I would create an extension method like this:

如果你想要没有标题的原始访问,我会创建一个像这样的扩展方法:

public static IEnumerable<TableRow> AllRows(this Table table)
{
     yield return new TableRow(table, table.Header);
     foreach(var row in table.Rows)
     {
         yield return row;
     }
 }

I don't know if you can create a TableRow (it looks like it should be possible from the source) like that and you might have to create your own TableRow object which wraps the specflow TableRow but you should get the idea.

我不知道你是否可以创建一个TableRow(它看起来应该可以从源代码中完成),你可能需要创建自己的TableRow对象来包装specflow TableRow但你应该明白这个想法。

#1


0  

It seems that SpecFlow always assumes a header. You can access this header with:

似乎SpecFlow总是采用标题。您可以访问此标头:

table.Header

to get raw access to the headings. This is just a collection of strings so you can access the individual elements with:

获得标题的原始访问权限。这只是一个字符串集合,因此您可以使用以下命令访问各个元素:

var firstRowFirstColumn = table.Header[0];
var firstRowSecondColumn = table.Header[1]; 

You can then just access the remaining individual rows in the table with something like this:

然后,您可以使用以下内容访问表中的其余各行:

foreach(var row in table.Rows)
{
   var first = row[0];
   var second = row[1];
   ... use the values ...
}

to get all of the other values in the table

获取表中的所有其他值

if you want raw access without the headers I would create an extension method like this:

如果你想要没有标题的原始访问,我会创建一个像这样的扩展方法:

public static IEnumerable<TableRow> AllRows(this Table table)
{
     yield return new TableRow(table, table.Header);
     foreach(var row in table.Rows)
     {
         yield return row;
     }
 }

I don't know if you can create a TableRow (it looks like it should be possible from the source) like that and you might have to create your own TableRow object which wraps the specflow TableRow but you should get the idea.

我不知道你是否可以创建一个TableRow(它看起来应该可以从源代码中完成),你可能需要创建自己的TableRow对象来包装specflow TableRow但你应该明白这个想法。