这个 例子)。

首先编写feature ,并保存为feature/addition.feature文件:

Feature: Addition
  In order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers
 
  Scenario Outline: Add two numbers
    Given I have entered <input_1> into the calculator
    And I have entered <input_2> into the calculator
    When I press <button>
    Then the result should be <output> on the screen
 
  Examples:
    | input_1 | input_2 | button | output |
    | 20 | 30 | add | 50 |

然后用Ruby语言定义scenario中的每个步骤 ,并保存为features/step_definitions/addition.rb文件:

[ruby] view plain copy
 
  1. Given /I have entered (/d+) into the calculator/ do |n|  
  2.   @calc = Calculator.new  
  3.   @calc.push n.to_i  
  4. end  
  5. When /I press (/w+)/ do |op|  
  6.   @result = @calc.send op  
  7. end  
  8. Then /the result should be (.*) on the screen/ do |result|  
  9.   @result.should == result.to_f  
  10. end   



最后编写相应代码 :

[ruby] view plain copy
 
  1. class Calculator  
  2.   def push(n)  
  3.     @args ||= []  
  4.     @args << n  
  5.   end  
  6.    
  7.   def add  
  8.     @args.inject(0){|n,sum| sum+=n}  
  9.   end  
  10. end   



进行测试 :
cucumber feature/addition.feature 

如果得到类似于以下的结果,则表明代码无误:
1 scenario (1 passed) 
4 steps (4 passed) 
0m0.009s 

否则,会得到红色提示,则需要修改相应代码。

相关文章:

  • 2022-12-23
  • 2021-10-16
  • 2021-08-19
  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
猜你喜欢
  • 2021-10-09
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案