Is there any way to specify eg (car|cars) in a cucumber step definition?

↘锁芯ラ 提交于 2020-03-25 12:33:20

问题


So I have 2 scenarios....one starts out

Given I have 1 car

The other starts out

Given I have 2 cars

I'd like them to use the same step definition - ie something like this

 Given('I have {int} (car|cars)',

I know it's possible to do specify 2 possible values (ie car or cars), but I can't for the life of me remember how. Does anyone know? I'm using typescript, protractor, angular, selenium.

Have also tried

Given(/^I have {int} (car|cars)$

回答1:


Within cukeExp, the () become optional characters. That is what you want.

So your expression would be

Given('I have {int} car(s)')

Happy to help - More information can be found here: https://cucumber.io/docs/cucumber/cucumber-expressions/ - Switch to JS code at the top.

Luke - Cucumber contributor.




回答2:


Luke's answer is great and is definitely standard practice when cuking.

I would (and do) take a different approach. I would strongly argue that the complexity of even a single expression like the one he uses isn't worth the step duplication. Let me explain and illustrate.

The fundamental idea behind this approach is that the internals of each step definition must be a single call to a helper method. When you do this you no longer need expressions or regex's.

I would prefer and use in my projects

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

to

Given('I have {int} car(s)')
 lots of stuff to create cars
end

because

  • the step definitions are simpler (no regex, no cucumber expression
  • the stuff to create the cars is slightly simpler (no processing of the regex or expression)
  • the helper method supports and encourages a wider range of expression e.g.
Given Fred has a car
Given there is a blue car and a red car
  • the helper method encourages better communication between steps because you can assign its results relative to the step definition e.g.
Given Fred has a car
 @freds_car = create_car
end

Given there are two cars
 [@car1, @car2] = create_car(amount: 2)
end

Cucumber expressions and cucumbers regex's are very powerful and quite easy to use, but you can Cuke very effectively without ever using them. Step definition efficiency is a myth and often an anti-pattern, if you ensure each step def is just a single call you no longer have to worry about it, and you will avoid the mistake many cukers fall into which is the writing over-complicated step definitions with lots of parameters, regex's and|or expressions.




回答3:


As far as I know, your step definition should be as below for it to work.

Given(/^I have "([^"]*)?" (car|cars)*$/, (number, item) => {

You can still simplify the first regular expression.

Cheers!



来源:https://stackoverflow.com/questions/60758919/is-there-any-way-to-specify-eg-carcars-in-a-cucumber-step-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!