繁体   English   中英

如何在C#中的Specflow中传递可选参数

[英]How to pass optional parameter in Specflow in C#

我在功能文件中有以下步骤

When I add employee with vendor 'vendorname'
And I add employee with vendor 'vendorname' and client 'clientname'

我希望在单步定义中执行这两个步骤,其中客户端名称可以是可选的。 我可以使用两步定义来实现这一点,但它会重复代码。

SpecFlow 不支持可选参数。
为此,您需要单独的步骤。

但我们欢迎为此提出 PR: https : //github.com/techtalk/SpecFlow/issues/316

所以你想写这样的东西

When I add an employee with the following information
| vendor     | client     |
| vendorname | clientname |

这将生成一个带有 Table 参数的步骤定义。

您可以自己旋转表或使用表 CreateInstance、CreateInstance<>、CreateSet、CreateSet<> 的辅助函数将表映射到测试中的对象。

好吧,我想您可以根据您想要实现的目标或您希望代码的外观来做一些事情。 现在我想到了一件事。

例如,而不是:

When I add employee with vendor 'vendorname'
And I add employee with vendor 'vendorname' and client 'clientname'

你可以写下一个:

When I add employee with vendor 'vendorname' and client 'clientname'

或者,应用与 Fran 提议的相同的方法:

When I add an employee with the following information
| vendor     | client     |
| vendorname | clientname |

然后,当您在 .feature 文件中的步骤中设置输入参数时,您可以选择将在步骤定义文件中查询的任何预定义字符串或值(使用 if 或 switch 语句)。

在行尾只能有一个可选参数。 使用非捕获组

[When(@"I add employee with vendor '(.*)'(?: and client '(.*))'")]
public void IAddEmptoyee(string vendorName, string clientName = null)
{
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM