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

时间:2019-01-08 11:04:06

标签: ruby regex cucumber optional-parameters optional-arguments

我可以执行以下步骤:

Then  I 'eat' all food except
   | Bread |

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

Then  I 'eat' all food

我决定将一些参数放入表中,因为否则可能很难读取它。

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)

它可以正常工作,但不适用于我不传递table参数的情况(“然后我'吃'所有食物”)。 可以将表参数设为可选吗?

1 个答案:

答案 0 :(得分:0)

我不确定它是否可以在黄瓜中使用,但是由于它只是一个红宝石块,因此可以尝试将spar与param一起使用。一个参数将包含整个参数列表,然后:

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