本节书摘来自异步社区《Cucumber:行为驱动开发指南》一书中的第2章,第2.4节,作者:【英】Matt Wynne , 【挪】Aslak Hellesy著,更多章节内容可以访问云栖社区“异步社区”公众号查看
我们已经确定计算器的第一个版本将会以命令行参数的形式接受用户输入,因此,对于Given the input “2+2”的步骤定义,我们的工作就是将输入记下来,以便下一步运行计算器的时候知道传入怎样的命令行参数。在features/step_definitions文件夹中,编辑文件calculator_steps.rb,修改第一个步骤定义如下:
下载first_taste/03/features/step_definitions/calculator_steps.rb Given /^the input "([^"]*)"$/ do |input| @input = input end这里我们所做的是将特性的输入保存在一个 Ruby 实例变量中,只要这个特定的场景还在运行,该实例变量就一直存在,因此我们可以在下一个步骤真正运行计算器的时候再次使用它。
不错,上一步很简单,现在我们该做什么呢?让我们问问cucumber:
Feature: Adding Scenario: Add two numbers Given the input "2+2" When the calculator is run TODO (Cucumber::Pending) ./features/step_definitions/calculator_steps.rb:9 features/adding.feature:5 Then the output should be "4" 1 scenario (1 pending) 3 steps (1 skipped, 1 pending, 1 passed) 0m0.003s耶!我们的第一个步骤通过了!当然,整个场景还是处于待定状态,因为我们还有另外两个步骤需要实现,不过我们已经开始有了一些进展。
相关资源:敏捷开发V1.0.pptx