《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就会停止运行当前场景并跳过该场景剩余的步骤。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值