黄瓜,从其他步骤调用步骤。最佳实践?

Cucumber, calling Steps from other step. Best practise?

今天和同事进行了深入的讨论。从另一个步骤调用一个步骤是否被认为是最佳实践? 所以,例如:

Given /a turtle/ do
  puts "turtle!"
end

Given /two turtles/ do
  step "a turtle"
  step "a turtle"
end

在我看来,这意味着您不能在不检查整个项目的情况下更改功能文件。因此,如果我想保持 DRY,我更喜欢使用从这些步骤调用的 "code" 函数(即在 Ruby 中)。

def turtle do
   puts "turtle!"
end

Given /a turtle/ do
  turtle
end

Given /two turtles/ do
  turtle
  turtle
end

如果别无选择,我什至更愿意复制代码而不是调用其他步骤。

Given /a turtle/ do
  puts "turtle!"
end

Given /two turtles/ do
  puts "turtle!"
  puts "turtle!"
end

什么是最佳实践,为什么?

嵌套步骤是一种非常糟糕的做法。如果你这样做,你很快就会陷入混乱。一个好的步骤定义是对方法的单行调用。其他任何事情都会很快变得混乱

考虑:

   When "I login" do
      visit login_path
      fill_in ...
      ...
      submit
   end

   When "I login as as admin" do
     visit admin_login_path
     fill_in ...
     ...
   end

看看有多少重复。现在您可以通过步骤嵌套和传递参数来删除它,但是您最终会得到一个非常复杂的步骤定义,很难调试,所以只需调用一个辅助方法

   When "I login" do
     login user: @i
   end

   When "I login as an admin" do 
     login user: @i, role: admin
   end

我们在这里所做的只是为一个简单的任务使用步骤定义,将场景中的一行转换为方法调用。所有复杂的东西现在都采用标准方法。编程语言擅长处理复杂性。当我们编写登录方法时,我们将拥有各种可用的工具和技术,因此我们可以在保持代码 DRY 的同时管理任何复杂性。

最好这样使用

特征

Given "2" turtles

步骤定义

Given / "(\d+)" turtles/ do | count |
  count.times.do
    puts "turtle!"
  end
end