阅读224 返回首页    go 阿里云 go 技术社区[云栖]


《Cucumber:行为驱动开发指南》——2.3 创建步骤定义

本节书摘来自异步社区《Cucumber:行为驱动开发指南》一书中的第2章,第2.3节,作者:【英】Matt Wynne , 【挪】Aslak Hellesy着,更多章节内容可以访问云栖社区“异步社区”公众号查看

2.3 创建步骤定义

先不要过多考虑之前Cucumber输出的代码片段是什么意思,我们先把这些代码复制并粘贴到一个Ruby文件中。和特性文件一样,Ruby期望在约定俗成的位置找到步骤定义:

$ mkdir features/step_definitions
现在在fetures/step_definitions目录下创建一个名为calculator_steps.rb的文件,只要这是一个Ruby文件,Cucumber并不介意你给这个文件起什么名字,但这里我们给这个文件起的名字其实不错。接着用编辑器打开该文件并粘贴下面的代码片段:

下载first_taste/02/features/step_definitions/calculator_steps.rb
Given /^the input "([^"]*)"$/ do |arg1|
 pending # express the regexp above with the code you wish you had
end

When /^the calculator is run$/ do
 pending # express the regexp above with the code you wish you had
end

Then /^the output should be "([^"]*)"$/ do |arg1|
 pending # express the regexp above with the code you wish you had
end

运行cucumber,它会告诉我们下一步做什么:

Feature: Adding

 Scenario: Add two numbers    
  Given the input "2+2"     
   TODO (Cucumber::Pending)
   ./features/step_definitions/calculator_steps.rb:2
   features/adding.feature:4
  When the calculator is run  
  Then the output should be "4" 

1 scenario (1 pending)
3 steps (2 skipped, 1 pending)
0m0.003s

场景已经从未定义(undefined)升级到了待定(pending)。这是个好消息,因为它说明Cucumber正在运行第一个步骤,不过在此过程中它撞上了我们复制并粘贴的那些步骤定义代码中的pending标记,pending的意思是告诉Cucumber这个场景还是一个正在进行中的工作。我们需要用真正的实现替换掉这个pending标记。

注意,Cucumber报告它跳过了另外两个步骤,只要遇到了失败的或者待定的步骤,Cucumber就会停止运行当前场景并跳过该场景剩余的步骤。

下面我们来实现第一个步骤定义。

最后更新:2017-06-05 11:34:01

  上一篇:go  《Cucumber:行为驱动开发指南》——2.4 实现第一个步骤定义
  下一篇:go  《Cucumber:行为驱动开发指南》——2.1 理解我们的目标