简体   繁体   English

将可选的表参数传递给黄瓜步骤(红宝石)

[英]Pass optional table parameter to a cucumber step (ruby)

I have a step that can be like this: 我有一个步骤可以像这样:

Then  I 'eat' all food except
   | Bread |

Then  I 'drink' all food except
   | Bread |
   | Milk  |

Then  I 'eat' all food

I decided to put some of the arguments into the table, because otherwise it may be hard to read it. 我决定将一些参数放入表中,因为否则可能很难读取它。

Step definition in Ruby is: Ruby中的步骤定义为:

Then(/^I '(eat|drink)' all food(?: except)?$/) do |action, exceptions|
  exceptions = exceptions.raw.flatten.map(&:strip) unless exceptions.nil?

  action == 'eat' ? method1(exceptions: exceptions) : method2(exceptions: exceptions)

It works fine, but not for the case when I don't pass the table argument ("Then I 'eat' all food"). 它工作正常,但不适用于我不传递表参数的情况(“然后我'吃'所有食物”)。 Is it possible to make a table argument optional? 是否可以将表参数设为可选?

I'm not sure if it will work in cucumber, but as it's just a ruby block you can try to use param with splat. 我不确定它是否可以在黄瓜中使用,但是由于它只是一个红宝石块,因此可以尝试将spar与param一起使用。 A single param will hold the whole parameter list then: 一个参数将保存整个参数列表,然后:

Then(/^I '(eat|drink)' all food(?: except)?$/) do |*params|
   # params is an array
   action = params[0]
   exceptions = params[1] # may be nil
   #...
end

More on splats 有关Splat的更多信息

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

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