数据列表作为specflow测试步骤中的参数

Mik*_*keW 3 c# regex bdd acceptance-testing specflow

我正在寻找一种方法来进行步骤定义,例如:

给出了1,2,3,4的数字集合

并使用int [],List或IEnumerable将其映射到步骤定义

正则表达式(\ d +(,\n +)*)匹配,但意味着我需要两个参数.

目前我有

[Given(@"a collection of numbers (\d+(,\d+)*)")]
public void givencollectionofnumbers(string p0, string p1)
{
    //p0 is "1,2,3,4"
    //p1 is ",4"
}
Run Code Online (Sandbox Code Playgroud)

我有一个简单的workarouns

[Given(@"a collection of numbers (.*)")]
public void givencollectionofnumbers(string p0)
{
    var numbers = p0.Split(',').Select(x => int.Parse(x));
}
Run Code Online (Sandbox Code Playgroud)

但我想以更优雅的方式做到这一点,可能会将数字的类型改为双打,并确保正则表达式仅列出数字列表.

我也不想使用表格,因为对于简单的数据列表来说似乎过多了

有人能帮忙吗

Oua*_*rzy 5

我只是在我的项目中解决了同样的问题:这将解决问题

((?:.,\d+)*(?:.\d+))
Run Code Online (Sandbox Code Playgroud)

如果你也想接受单个int,请改用:

((?:.,\d+)*(?:.+))
Run Code Online (Sandbox Code Playgroud)

你的命题有两个问题:

  • Specflow尝试将它作为2个参数匹配,当你只需要1.但是我无法在文档中找到它为什么这样做的明确解释

  • 你肯定需要一个StepArgumentTransformation来转换任何可枚举的输入字符串

所以你的最后一步功能将如下所示:

[Given(@"foo ((?:.,\d+)*(?:.+))")]
public void Foo(IEnumerable<int> ints)
{
}

[StepArgumentTransformation(@"((?:.,\d+)*(?:.+))")]
public static IEnumerable<int> ListIntTransform(string ints)
{
    return ints.Split(new[] { ',' }).Select(int.Parse);
}
Run Code Online (Sandbox Code Playgroud)

并且在Foo函数中收到一个Enumerable int.

希望能帮助到你.