有什么方法可以在黄瓜步骤定义中指定 eg (car|cars) 吗?
Is there any way to specify eg (car|cars) in a cucumber step definition?
所以我有 2 个场景....一个开始
Given I have 1 car
另一个开始
Given I have 2 cars
我希望他们使用相同的步骤定义 - 即像这样
Given('I have {int} (car|cars)',
我知道可以指定 2 个可能的值(即 car 或 cars),但我想不起来具体是怎么做的。有人知道吗?我正在使用打字稿、量角器、angular、硒。
也试过
Given(/^I have {int} (car|cars)$
据我所知,您的步骤定义应该如下所示才能正常工作。
Given(/^I have "([^"]*)?" (car|cars)*$/, (number, item) => {
您仍然可以简化第一个正则表达式。
干杯!
在 cukeExp 中,() 成为可选字符。这就是你想要的。
所以你的表达方式是
Given('I have {int} car(s)')
乐于提供帮助 - 可以在此处找到更多信息:https://cucumber.io/docs/cucumber/cucumber-expressions/ - 切换到顶部的 JS 代码。
卢克 - 黄瓜贡献者。
Luke 的回答很好,绝对是标准做法。
我会(并且会)采取不同的方法。我强烈认为即使像他使用的那样的单个表达式的复杂性也不值得重复步骤。让我来解释和说明。
此方法背后的基本思想是每个步骤定义的内部必须是对辅助方法的单个调用。当您这样做时,您不再需要表达式或正则表达式。
我更愿意在我的项目中使用
module CarStepHelper
def create_car(amount: 1)
Lots of stuff to create cars
end
end
World CarStepHelper
Given 'I have one car' do
create_car
end
Given 'I have two cars' do
create_car(amount: 2)
end
到
Given('I have {int} car(s)')
lots of stuff to create cars
end
因为
- 步骤定义更简单(没有正则表达式,没有黄瓜表达式
- 创建汽车的东西稍微简单一些(不处理正则表达式或表达式)
- 辅助方法支持并鼓励更广泛的表达方式,例如
Given Fred has a car
Given there is a blue car and a red car
- 辅助方法鼓励步骤之间更好的通信,因为您可以相对于步骤定义分配其结果,例如
Given Fred has a car
@freds_car = create_car
end
Given there are two cars
[@car1, @car2] = create_car(amount: 2)
end
Cucumber 表达式和 cucumbers 正则表达式非常强大且非常易于使用,但您无需使用它们就可以非常有效地进行 Cuke。步骤定义效率是一个神话,通常是一种反模式,如果你确保每个步骤定义只是一个单一的调用,你就不再需要担心它,你将避免许多 cukers 犯的错误,即编写过于复杂包含大量参数、正则表达式和|或表达式的步骤定义。
所以我有 2 个场景....一个开始
Given I have 1 car
另一个开始
Given I have 2 cars
我希望他们使用相同的步骤定义 - 即像这样
Given('I have {int} (car|cars)',
我知道可以指定 2 个可能的值(即 car 或 cars),但我想不起来具体是怎么做的。有人知道吗?我正在使用打字稿、量角器、angular、硒。
也试过
Given(/^I have {int} (car|cars)$
据我所知,您的步骤定义应该如下所示才能正常工作。
Given(/^I have "([^"]*)?" (car|cars)*$/, (number, item) => {
您仍然可以简化第一个正则表达式。
干杯!
在 cukeExp 中,() 成为可选字符。这就是你想要的。
所以你的表达方式是
Given('I have {int} car(s)')
乐于提供帮助 - 可以在此处找到更多信息:https://cucumber.io/docs/cucumber/cucumber-expressions/ - 切换到顶部的 JS 代码。
卢克 - 黄瓜贡献者。
Luke 的回答很好,绝对是标准做法。
我会(并且会)采取不同的方法。我强烈认为即使像他使用的那样的单个表达式的复杂性也不值得重复步骤。让我来解释和说明。
此方法背后的基本思想是每个步骤定义的内部必须是对辅助方法的单个调用。当您这样做时,您不再需要表达式或正则表达式。
我更愿意在我的项目中使用
module CarStepHelper
def create_car(amount: 1)
Lots of stuff to create cars
end
end
World CarStepHelper
Given 'I have one car' do
create_car
end
Given 'I have two cars' do
create_car(amount: 2)
end
到
Given('I have {int} car(s)')
lots of stuff to create cars
end
因为
- 步骤定义更简单(没有正则表达式,没有黄瓜表达式
- 创建汽车的东西稍微简单一些(不处理正则表达式或表达式)
- 辅助方法支持并鼓励更广泛的表达方式,例如
Given Fred has a car
Given there is a blue car and a red car
- 辅助方法鼓励步骤之间更好的通信,因为您可以相对于步骤定义分配其结果,例如
Given Fred has a car
@freds_car = create_car
end
Given there are two cars
[@car1, @car2] = create_car(amount: 2)
end
Cucumber 表达式和 cucumbers 正则表达式非常强大且非常易于使用,但您无需使用它们就可以非常有效地进行 Cuke。步骤定义效率是一个神话,通常是一种反模式,如果你确保每个步骤定义只是一个单一的调用,你就不再需要担心它,你将避免许多 cukers 犯的错误,即编写过于复杂包含大量参数、正则表达式和|或表达式的步骤定义。