Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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# specflow测试步骤中作为参数的数字列表_C#_Regex_Bdd_Specflow_Acceptance Testing - Fatal编程技术网

C# specflow测试步骤中作为参数的数字列表

C# specflow测试步骤中作为参数的数字列表,c#,regex,bdd,specflow,acceptance-testing,C#,Regex,Bdd,Specflow,Acceptance Testing,我正在寻找一种方法来定义步骤,例如: 给定一组数字1,2,3,4 并使用int[]、List或IEnumerable将其映射到步骤定义 正则表达式(\d+(,\d+*)匹配,但意味着我需要两个参数 目前我有 [Given(@"a collection of numbers (\d+(,\d+)*)")] public void givencollectionofnumbers(string p0, string p1) { //p0 is "1,2,3,4" //p1 is ",

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

给定一组数字1,2,3,4

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

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

目前我有

[Given(@"a collection of numbers (\d+(,\d+)*)")]
public void givencollectionofnumbers(string p0, string p1)
{
    //p0 is "1,2,3,4"
    //p1 is ",4"
}
我有一个简单的解决办法就是

[Given(@"a collection of numbers (.*)")]
public void givencollectionofnumbers(string p0)
{
    var numbers = p0.Split(',').Select(x => int.Parse(x));
}
但我想以一种更优雅的方式来实现这一点,可能会将数字的类型更改为双倍,同时也确保正则表达式只处理数字列表

我也不希望使用表来处理这个问题,因为它对于简单的数据列表来说似乎太过分了


有人能帮我解决这个问题吗?

我只是在我的项目上解决了同样的问题:这样就可以了

((?:.,\d+)*(?:.\d+))
如果您还想接受单整数,请使用以下选项:

((?:.,\d+)*(?:.+))
你的提议有两个问题:

  • 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);
}
[给定(@“foo((?:,\d+)*(?:。+))”)
公共void Foo(IEnumerable int)
{
}
[StepArgumentTransformation(@)(((:,\d+)*(((?:.+)))”)
公共静态IEnumerable ListIntTransform(字符串ints)
{
返回int.Split(new[]{',});
}
然后在Foo函数中接收一个可枚举的int


希望有帮助。

我遇到了类似的情况,下面是我们的想法。关键问题是SpecFlow看到的每个“(”都会创建一个新参数。在这种情况下,只需添加一个非捕获组“(?:”

正如Ouzrzy在他的回答中所写的那样,您可以添加一个step arg转换以使签名更简单,或者在步骤中这样做,正如我在下面记录的那样

句子

Scenario: All of these sentences match below definition
    Then The quick red fox jumped over '10, 20, 30' lazy dogs
    Then the quick brown fox jumped over '10,20' lazy dogs
    Then the quick brown fox jumped over '1' lazy dog
最佳实践:所有参数(包括INT)都应该用单引号括起来。这可以提高可读性并减少不明确匹配的可能性

定义

[Then(@"(?i)t(?-i)he quick (?:brown|red) fox jumped over '(?x)((?:\d+,)*\d+)(?-x)' lazy dogs?")]
public void ThenTheQuickBrownFoxJumpedOverLazyDog(string countCsv)
{
    Console.WriteLine("sum of " + countCsv + " = ");
    var countCollection = countCsv.Split(',').Select(int.Parse);
    Console.WriteLine(countCollection.Sum());
}

最佳实践:永远不要保留自动定义的参数名。您的参数名不仅应该描述其类型(int、string等),还应该描述您期望的数据类型(计数、文件名等).

您是否希望它也匹配像
1、2、3、4这样的字符串?如果其中有alpha,会发生什么情况;是否可以忽略它(例如
1、2、3、4、a
)?我只想匹配数字列表,alpha与正则表达式不匹配,因此可以忽略yes,如果可能,我希望匹配的参数是类型安全的。空格不太重要,但添加一个\s*可能是个好主意。我的问题不是实际的正则表达式与patterm匹配,而是它与t的映射通过SpecFlow查看步骤定义的参数您的“变通方法”没有问题。我不确定如何将集合作为参数更优雅。将解析更改为double与集合参数一样简单,如果列表包含非数字,测试将失败,我认为这是预期结果。这看起来是一个很好的正则表达式,但除了验证fo之外,我看不出这有什么好处对于字符串的rmat,方法的参数仍然需要是字符串,而不是IEnumerable或(params)int[]理想情况下,我会更新我的答案,因为我认为我不够清楚。通过这种方式,你可以在你的spec函数中得到一个可枚举的int。我猜你一开始没有使用StepArgumentTransformation,但我非常确定这是实现这一点的唯一方法。太棒了,谢谢!这太简洁了,用在一步定义中l、 但是我有很多步骤需要一系列整数到一步的参数转换,因为它们都很好。(?:\d+,)*\d+(-x)可能会更好,因为(?x)表示忽略空格,并且\d强制你只接受数字。'10,20'或'10,20'应该在这里匹配。