Cucubmer 没有when

上一篇 / 下一篇  2013-01-14 10:45:51 / 个人分类:测试

呵呵,今天突然想试试看,如果没有when的cucubmer是不是还能跑的通。

这是原始文件:

# language: zh-CN
功能:加法
  为了避免一些愚蠢的错误
  作为一个数学白痴
  我希望有人告诉我数字相加的结果

  场景: 两个数相加
    假如我已经在计算器里输入6
    而且我已经在计算器里输入7
    当我按相加按钮
    那么我应该在屏幕上看到的结果是13

  场景: 三个数相加
    假如我已经在计算器里输入6
    而且我已经在计算器里输入7
    而且我已经在计算器里输入1 
    当我按相加按钮
    那么我应该在屏幕上看到的结果是14

相应的define step文件是:

# encoding: utf-8
begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
require 'cucumber/formatter/unicode'
$:.unshift(File.dirname(__FILE__) + '/../../lib') 
require 'calculator'

Before do
  @calc = Calculator.new
end

After do
end

Given /我已经在计算器里输入(\d+)/ do |n|
  @calc.push n.to_i
end

When /我按(.*)按钮/ do |op|
  if p == '相加'
    @result = @calc.send "add"
  end
end

Then /我应该在屏幕上看到的结果是(.*)/ do |result|
  @result.should == result.to_f
end

这是原始例子 可以正常跑通:

2 scenarios (2 passed)
9 steps (9 passed)
0m0.016s



这是没有when的文件:

# language: zh-CN
功能:加法
  为了避免一些愚蠢的错误
  作为一个数学白痴
  我希望有人告诉我数字相加的结果

  场景: 两个数相加
    假如我已经在计算器里输入6
    而且我已经在计算器里输入7
    而且我按相加按钮
    那么我应该在屏幕上看到的结果是13

  场景: 三个数相加
    假如我已经在计算器里输入6
    而且我已经在计算器里输入7
    而且我已经在计算器里输入1 
    而且我按相加按钮
    那么我应该在屏幕上看到的结果是14

相应修改的step define文件是:

# encoding: utf-8
begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
require 'cucumber/formatter/unicode'
$:.unshift(File.dirname(__FILE__) + '/../../lib') 
require 'calculator'

Before do
  @calc = Calculator.new
end

After do
end

Given /我已经在计算器里输入(\d+)/ do |n|
  @calc.push n.to_i
end

Given /我按(.*)按钮/ do |op|
  if p == '相加'
    @result = @calc.send "add"
  end
end

Then /我应该在屏幕上看到的结果是(.*)/ do |result|
  @result.should == result.to_f
end

也可以正常跑通:

2 scenarios (2 passed)
9 steps (9 passed)
0m0.016s


其实为了更好的能够分隔Given和Then,还是加一个什么都不做的When比较好:
# language: zh-CN
功能:加法
  为了避免一些愚蠢的错误
  作为一个数学白痴
  我希望有人告诉我数字相加的结果

  场景: 两个数相加
    假如我已经在计算器里输入6
    而且我已经在计算器里输入7
    而且我按相加按钮
    当我开始验证结果
    那么我应该在屏幕上看到的结果是13

  场景: 三个数相加
    假如我已经在计算器里输入6
    而且我已经在计算器里输入7
    而且我已经在计算器里输入1 
    而且我按相加按钮
    当我开始验证结果
    那么我应该在屏幕上看到的结果是14

相应的step define 文件是:

# encoding: utf-8
begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
require 'cucumber/formatter/unicode'
$:.unshift(File.dirname(__FILE__) + '/../../lib') 
require 'calculator'

Before do
  @calc = Calculator.new
end

After do
end

Given /我已经在计算器里输入(\d+)/ do |n|
  @calc.push n.to_i
end

Given /我按(.*)按钮/ do |op|
  if p == '相加'
    @result = @calc.send "add"
  end
end

When /我开始验证结果/ do ||
  puts '我开始验证结果'
end

Then /我应该在屏幕上看到的结果是(.*)/ do |result|
  @result.should == result.to_f
end



正常跑通:

2 scenarios (2 passed)
11 steps (11 passed)
0m0.016s

说明,cucumber是完全按照关键词+行去进行正则表达式的匹配的,各个步骤之间并没有逻辑关系,只是通过你的组织,实现了产品本身的业务逻辑。

TAG:

 

评分:0

我来说两句

日历

« 2024-05-03  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 151611
  • 日志数: 185
  • 文件数: 6
  • 建立时间: 2007-08-06
  • 更新时间: 2015-01-06

RSS订阅

Open Toolbar