我可以在黄瓜步骤中将int和float相同吗?

时间:2019-02-28 22:51:41

标签: cucumber

我正在尝试为我用Ruby编写的Tuple类编写测试(这是学习Ruby和Gherkin的练习)。因此,我的一个场景创建了一个带有浮点值的元组:

Scenario: A tuple with w=1.0 is a point
  Given a ← tuple[4.3, -4.2, 3.1, 1.0]
   Then a.x = 4.3
    And ...

对于Given步骤,黄瓜建议以下内容:

Given("a ← tuple[{float}, {float}, {float}, {float}]") do |float, float2, float3, float4|
  pending # Write code here that turns the phrase above into concrete actions
end

我实现为:

Given("a ← tuple[{float}, {float}, {float}, {float}]") do |float, float2, float3, float4|
  tuple_a = Tuple.new(float, float2, float3, float4)
end

太好了。现在,我需要另一个将整数传递给元组的方案:

 Scenario: Adding two tuples
   Given a ← tuple[3, -2, 5, 1]
   ...

黄瓜建议:

Given("a ← tuple[{int}, {int}, {int}, {int}]") do |int, int2, int3, int4|
  pending # Write code here that turns the phrase above into concrete actions
end

但是我的实现是在Ruby中进行的;我不太在乎是否要将intfloat传递给Tuple.new()。我首先实现了Given步骤,期望浮点数对于int可以工作,但是Cucumber不会使用它。它希望我再次使用int参数来实现它。我可以只使用浮点参数,例如Given a ← tuple[3.0, -2.0, 5.0, 1.0],但这很烦人。我是定义自定义ParameterType的唯一选择吗?这将需要同时匹配整数和浮点数的正则表达式;会优先于现有的intfloat类型吗?

1 个答案:

答案 0 :(得分:2)

我建议对此类事情使用单元测试工具,例如rspec,minitest等。它们非常适合。

当您可以使用非技术性和抽象性的语言来表达事物时,方案非常有用。您的方案既技术又具体,很难阅读和编写。

一个类比试图用一种自然的语言编写数学表达式。

(3+5)^3add 5 to 3 and then cube the total

更简单,更精确

学习黄瓜的艺术是如何编写简单明了的场景来描述特定的行为。它与使用多个参数,复杂的正则表达式,大型表和多个示例无关。如果您想学习如何Cuke和进行BDD,则您正在学习错误的东西。如果您想学习红宝石并为Tuple类之类的东西编写测试,则会使用错误的工具。

相关问题