我可以在 Cucumber Steps 中同样对待 int 和 float 吗?

Can I treat int and float the same in my Cucumber Steps?

我正在尝试为我在 Ruby 中编写的元组 class 编写测试(这是一个同时学习 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 步骤,cucumber 建议如下:

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;我真的不在乎我是否将 ints 或 floats 传递给 Tuple.new()。我首先实现的 Given 步骤期望浮点数对 ints 起作用,但 Cucumber 不会使用它;它要我用 int 参数再次实现它。我可以只使用浮点参数,例如Given a ← tuple[3.0, -2.0, 5.0, 1.0],但这有点烦人。是我定义自定义 ParameterType 的唯一选择吗?这将需要一个匹配整数和浮点数的正则表达式;这会优先于现有的 intfloat 类型吗?

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

当您可以用非技术性和抽象性的语言表达事物时,场景就很有用。您的场景是技术性的和具体的,并且更难阅读和编写。

一个类比是尝试用自然语言编写数学表达式。

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

更简单、更精确

学习 Gherkin 的艺术是如何编写描述特定行为的简单清晰的场景。它不是关于使用多个参数、复杂的正则表达式、大表和多个示例。如果你想学习如何 Cuke 和做 BDD,你就在学习错误的东西。如果您想学习 ruby 并为元组 class.

之类的东西编写测试,那么您使用了错误的工具