简体   繁体   English

如何生成列表<T>在 SpecFlow 场景的步骤定义中?

[英]How do I generate a List<T> in the step definition of a SpecFlow Scenario?

In the documentation for SpecFlow Step Definitions it shows an example where you can create a list in the step definition by using a comma separated list in the scenario:在 SpecFlow步骤定义的文档中,它显示了一个示例,您可以在该示例中使用场景中的逗号分隔列表在步骤定义中创建列表:

For example:例如:

Given I am available on "Tuesday,Friday,Sunday"鉴于我在“周二、周五、周日”有空

would result in:会导致:

@Given("I am available on \"(.+)\"")
public void I_have_cukes_in_my_belly(List<String> days) {
    // Do something with the days
}

Unfortunately, when I do it, I get this:不幸的是,当我这样做时,我得到了这个:

@Given("I am available on ""(.*)"")
public void I_have_cukes_in_my_belly(string days) {
    // Do something with the days
}

I am using VS 2012 and version 1.9.1 of SpecFlow.我正在使用 VS 2012 和 SpecFlow 1.9.1 版。 Does anyone have any idea what I might be doing wrong?有谁知道我可能做错了什么? Or how I can go about fixing this?或者我该如何解决这个问题?

Thanks in advance.提前致谢。

You could do something like:你可以这样做:

Given I am available on these days:鉴于我这几天有空:

| | DaysOfWeek |星期几 |

| | Monday |星期一 |

| | Tuesday |星期二 |

| | Wednesday |星期三 |

Then you could use the table and convert that to a list...然后你可以使用表格并将其转换为列表......

@Given("I am available on these days:")
public void I_have_cukes_in_my_belly(Table table) {
    List<string> days = new List<string>();
    foreach (var tableRow in table.Rows)
    {
        days.Add(tableRow["DaysOfWeek"]);
    }
 }

The only language that seems to support Lists currently is Java.目前似乎支持列表的唯一语言是 Java。 I'm assuming you're using C# as you're using VS2012.我假设您在使用 VS2012 时使用 C#。 If you switch the code sample on your linked page to C# instead of Java, the List example goes away and I'm seeing the same behavior you are.如果您将链接页面上的代码示例切换到 C# 而不是 Java,List 示例就会消失,我看到的行为与您相同。

However, you can simply get the string as a parameter and put it into a List.但是,您可以简单地获取字符串作为参数并将其放入列表中。 1 line. 1 行。

C# C#

[Given(@"I am available on ""(.*)""")]
public void GivenIAmAvailableOn(string days)
{
        List<String> daysList = new List<string>(days.Split(','));

您可以尝试查询 Table 实例以获取您要查找的列表。

expectedTable.Rows.Select(row => row.Values.FirstOrDefault()).ToList()

If you click C# on the Step Definitions page you will see that the List examples disappear.如果在Step Definitions页面上单击 C#,您将看到 List 示例消失了。 However as OCary points out you can simulate it by splitting the string.但是,正如 OCary 指出的那样,您可以通过拆分字符串来模拟它。

But I would suggest that there is no need to cast back to list and you can just use IEnumerable via Linq instead so you can parse the list more simply ie但我建议没有必要转换回列表,您可以通过 Linq 使用 IEnumerable 代替,这样您就可以更简单地解析列表,即

[Given(@"I am available on ""(.*)""")]
public void GivenIAmAvailableOn(string days)
{
    var daysList = from x in days.Split(',') select x.Trim();

Currently I'm using a step argument transformation to get the list right:目前我正在使用步骤参数转换来获得正确的列表:

[StepArgumentTransformation(@"((?:.*)(?:,.*)*)")]
public List<string> StringToList(string list)
{
    // (
    //    (?:    - Don't capture
             .*  - Match any character
    //    )
    //    (?:    - Don't capture
    //       ,   - Separated by a comma
    //       .*  - Match any character
    //    )*     - Zero or more items after the first item
    // )

    return list
        .Split(',')
        .Select(x => x.Trim())
        .ToList();
}

Then you'd be able to use List<string> as the argument type, like so:然后您就可以使用List<string>作为参数类型,如下所示:

[Given(@"I am available on ""(.*)""")]
public void GivenIAmAvailableOn(List<string> days)
{
}

Use Table to IEnumerable<T> convertor:使用TableIEnumerable<T>转换器:

[StepArgumentTransformation]
public static IEnumerable<string> ToStringEnumerable(Table table) =>
    table.Rows.Select(t => t.Values.Single());

Then you can use it as in your example (or cast to List<T> if you like):然后您可以像在您的示例中一样使用它(或者如果您愿意,可以转换为List<T> ):

@Given("I am available on \"(.+)\"")
public void I_have_cukes_in_my_belly(IEnumerable<String> days) {
    // Do something with the days
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从SpecFlow中的场景获取当前的步骤定义 - Get current Step definition from of a Scenario in SpecFlow 无法从SpecFlow步骤定义内部调用Given,When或Then - Can't call Given, When or Then from inside a SpecFlow step definition Specflow没有生成正确的步骤定义 - Specflow not generating proper step definition 如何从 specflow 的表中将整数添加到场景和功能文件中? 为什么我会收到棕褐色错误 - How to add integers to scenario and feature files from a table in specflow? Why do I get tan error 如何适应 specflow 步骤定义以使用一种方法从 specflow 特征文件中接受不同数量的参数 - How to accommodate specflow step definition to accept deferent number of parameters from specflow feature file with one method Specflow Scenario Outline不生成预期的步骤代码 - Specflow Scenario Outline not generating expected step code Specflow 2022 生成步骤定义文件 - VSC BDD:所有步骤都已在此文件中定义 - Specflow 2022 Generate a Step Definition file - VSC BDD: All steps have been defined in this file already Specflow:如何创建一个共享步骤来规则所有登录到具有多个用户角色的系统? - Specflow : How do I create a single shared step to rule all Logins to a system with multiple user roles? 如何正确构造此SpecFlow功能/场景集? - How would I structure this SpecFlow Feature/Scenario set correctly? 具有通配符属性的Specflow步骤定义映射 - Specflow step definition mapping with wildcard attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM