Cucumber 正则表达式匹配黄瓜,但不捕获它

Cucumber 正则表达式匹配黄瓜,但不捕获它,cucumber,specflow,gherkin,Cucumber,Specflow,Gherkin,我正在使用specflow使用Gherkin语法编写浏览器测试。我有一个步骤定义,我想匹配两个不同的步骤,但不捕获它。例如: Scenario: Given I have some stuff And I click on the configure user And I configure user And I set the user <config> to <value> Then I should see user configuration

我正在使用
specflow
使用
Gherkin
语法编写浏览器测试。我有一个步骤定义,我想匹配两个不同的步骤,但不捕获它。例如:

Scenario:
  Given I have some stuff
  And I click on the configure user 
  And I configure user
  And I set the user <config> to <value>
  Then I should see user configuration is updated

Scenario:
  Given I have some stuff
  And I click on the configure admin
  And I configure admin
  And I set the admin <config> to <value>
  Then I should see user configuration is updated
对于
,我将用户设置为
,如下所示:

Given(@"And I set the admin (.*) to (.*)")
public void AndISetTheUserConfigToValue(string config, string value) 
{
    // implementation
}
这两个步骤的实现是相同的。所以我想做的是:

Given(@"And I set the user|admin (.*) to (.*)")
public void AndISetTheConfigToValue(string config, string value) 
{
    // implementation
}
上述代码将不会作为
config
工作,
value
参数将是空字符串,因为
user
admin
被捕获为前两个参数

有没有一种方法可以在不捕获参数中的正则表达式匹配项的情况下执行上述操作?

我知道我可以简单地将场景改写为以下内容来解决问题。但我只是好奇

Scenario:
  Given I have some stuff
  And I click on the configure admin
  And I configure admin
  And I set the <config> to <value>
  Then I should see user configuration is updated
场景:
因为我有一些东西
然后我点击配置管理员
我配置了管理员
然后我把灯调好
然后我会看到用户配置被更新

首先要小心,在同一个绑定中有多个
(.*)
会导致捕获错误的模式

如果不检查,我很确定可以在方法上提供多个绑定,只要它们具有相同的参数计数,即

 [Given("I set the user (.*) to (.*)"]
 [Given("I set the admin (.*) to (.*)"]
 public void ISetTheConfigValue(string config, string value)  
或者,始终可以添加虚拟参数

 [Given("I set the (user|admin) (.*) to (.*)"]
 public void ISetTheConfigValue(string _, string config, string value)

首先要小心在同一个绑定中有多个
(.*)
s,因为这可能会导致捕获错误的模式

如果不检查,我很确定可以在方法上提供多个绑定,只要它们具有相同的参数计数,即

 [Given("I set the user (.*) to (.*)"]
 [Given("I set the admin (.*) to (.*)"]
 public void ISetTheConfigValue(string config, string value)  
或者,始终可以添加虚拟参数

 [Given("I set the (user|admin) (.*) to (.*)"]
 public void ISetTheConfigValue(string _, string config, string value)

使用AlSki提供的作为基线:

使用可选组也是此处的一个选项:

[Given(@"I set the (?:user|admin) (.*) to (.*)"]
public void ISetTheConfigValue(string config, string value)
这意味着您不必包含一个永远不会使用的参数

我建议去掉讨厌的
(.*)
regex,它将匹配您放在其中的任何东西-如果您想采取一个步骤来获取该用户可以拥有的特权,这将是一个问题(例如):

给定我将用户JohnSmith设置为具有示例权限的管理员

所以我个人会用这个:

[Given(@'I set the (?:user|admin) "([^"]*)" to "([^"]*)"']
public void ISetTheConfigValue(string config, string value)
哪一个匹配:

Given I set the user "JohnSmith" to "SysAdmin"
And I set the admin "JaneDoe" to "User"
但并不匹配

Given I set the user JohnSmith to an admin with example privileges

使用AlSki提供的作为基线:

使用可选组也是此处的一个选项:

[Given(@"I set the (?:user|admin) (.*) to (.*)"]
public void ISetTheConfigValue(string config, string value)
这意味着您不必包含一个永远不会使用的参数

我建议去掉讨厌的
(.*)
regex,它将匹配您放在其中的任何东西-如果您想采取一个步骤来获取该用户可以拥有的特权,这将是一个问题(例如):

给定我将用户JohnSmith设置为具有示例权限的管理员

所以我个人会用这个:

[Given(@'I set the (?:user|admin) "([^"]*)" to "([^"]*)"']
public void ISetTheConfigValue(string config, string value)
哪一个匹配:

Given I set the user "JohnSmith" to "SysAdmin"
And I set the admin "JaneDoe" to "User"
但并不匹配

Given I set the user JohnSmith to an admin with example privileges

场景的重写看起来与原始场景相同。标记格式是否删除了一些字符?场景的重写看起来与原始场景相同。降价格式删除了一些字符吗?我想我是在找
[给定(@'我将(?:user | admin)([^“]*)”设置为“([^”]*)”]
。谢谢我想我是在找
[给定(@'我将(?:user | admin)([^“]*)”设置为“([^”]*)”]
。谢谢