BT

如何利用碎片时间提升技术认知与能力? 点击获取答案

RSpec增加了众所翘首以待的RBehave功能以供集成测试所需

| 作者 Sean Miller 关注 0 他的粉丝 ,译者 张海龙 关注 0 他的粉丝 发布于 2007年11月1日. 估计阅读时间: 11 分钟 | CNUTCon 了解国内外一线大厂50+智能运维最新实践案例。

RSpec是 一个为Ruby编写的基于行为驱动开发(简称BDD,即Behaviour-Driven Development)的验收测试框架,同时也可用于Java(事实上它一直都可以运行于JRuby中),它提供了一种机制,即由开发人员从业务中获取 验收标准并将它们转换为可读、可运行的示例,以此替代文档、测试和适用于业务的构建报告。

尽管RSpec对单元级测试很有用,但它在集成测试中一直存在一个盲点。Dan North创建了一个独立的扩展,RBehave,它用Given…With…Then…这样的格式以一系列的步骤从故事级别来描述行为。(North最早是在JBehave中描述了这种用于获得需求故事的模式)

David Chelimsky现在已经向RSpec trunk中合并了一个纯文本故事运行器(Plain Text Story Runner),它给RSpec添加了RBehave功能,就像他 他的 博客中描述的那样。

现在看看North的经典RBehave示例:

So, North's classic RBehave example:
require ‘rubygems’
require ‘rbehave’
require ’spec’ # for "should" method

require ‘account’ # the actual application code

Story "transfer to cash account",
%(As a savings account holder
  I want to transfer money from my savings account
  So that I can get cash easily from an ATM) do

  Scenario "savings account is in credit" do
    Given "my savings account balance is", 100 do |balance|
      @savings_account = Account.new(balance)
    end
    Given "my cash account balance is", 10 do |balance|
      @cash_account = Account.new(balance)
    end
    When "I transfer", 20 do |amount|
      @savings_account.transfer_to(@cash_account, amount)
    end
    Then "my savings account balance should be", 80 do |expected_amount|
      @savings_account.balance.should == expected_amount
    end
    Then "my cash account balance should be", 30 do |expected_amount|
      @cash_account.balance.should == expected_amount
    end
  end

  Scenario "savings account is overdrawn" do
    Given "my savings account balance is", -20
    Given "my cash account balance is", 10
    When "I transfer", 20
    Then "my savings account balance should be", -20
    Then "my cash account balance should be", 10
  end
end

在新的RSpec中它可以变成这样,由一个Ruby文件定义可用的步骤:

class AccountSteps < Spec::Story::StepGroup
  steps do |define|
    define.given("my savings account balance is $balance") do |balance|
      @savings_account = Account.new(balance.to_f)
    end

    define.given("my cash account balance is $balance" do |balance|
      @cash_account = Account.new(balance.to_f)
    end

    define.then("my savings account balance should be $expected_amount" do |expected_amount|
      @savings_account.balance.should == expected_amount.to_f
    end

    define.then("my cash account balance should be $expected_amount" do |expected_amount|
      @cash_account.balance.should == expected_amount.to_f
    end
  end
end

steps = AccountSteps.new do |define|
  define.when("I transfer $amount") do |amount|
    @savings_account.transfer_to(@cash_account, amount.to_f)
  end
end
在一个纯文本文件中按照那些步骤定义故事的行为:
Story: transfer to cash account
  As a savings account holder
  I want to transfer money from my savings account
  So that I can get cash easily from an ATM

  Scenario: savings account is in credit
    Given my savings account balance is 100
    And my cash account balance is 10
    When I transfer 20
    Then my savings account balance should be 80
    And my cash account balance should be 30

  Scenario: savings account is overdrawn
    Given my savings account balance is -20
    And my cash account balance is 10
    When I transfer 20
    Then my savings account balance should be -20
    And my cash account balance should be 10

由一个Ruby文件将他们粘在一起,并运行这些故事:

require 'spec'
require 'path/to/your/library/files'
require 'path/to/file/that/defines/account_steps.rb'

# assumes the other story file is named the same as this file minus ".rb"
runner = Spec::Story::Runner::PlainTextStoryRunner.new(File.expand_path(__FILE__).gsub(".rb",""))
runner.steps << AccountSteps.new
runner.run

纯文本文件中的那些步骤描述,必须与StepGroup中定义的步骤相匹配,这些描述可能会随着步骤数量的增加变得难以理解。Aslak Hellesøy正在为一个基于浏览器的编缉器工作,它将提供步骤的自动补全,并可以在恰当的位置对参数进行编辑,从而使这一问题得以简化。

查看英文原文RSpec Trunk Now Includes RBehave-like Story Runner

评价本文

专业度
风格

您好,朋友!

您需要 注册一个InfoQ账号 或者 才能进行评论。在您完成注册后还需要进行一些设置。

获得来自InfoQ的更多体验。

告诉我们您的想法

允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p

当有人回复此评论时请E-mail通知我

RSpec 用起来还是相当棒的 by cao Raecoo

RSpec 用起来还是相当棒的
特别是以测试代码来表现文档,不需要再去特意撰写相关文档
希望有机会可以和大家一起交流
raecoo AT gmail.com

允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p

当有人回复此评论时请E-mail通知我

允许的HTML标签: a,b,br,blockquote,i,li,pre,u,ul,p

当有人回复此评论时请E-mail通知我

1 讨论

登陆InfoQ,与你最关心的话题互动。


找回密码....

Follow

关注你最喜爱的话题和作者

快速浏览网站内你所感兴趣话题的精选内容。

Like

内容自由定制

选择想要阅读的主题和喜爱的作者定制自己的新闻源。

Notifications

获取更新

设置通知机制以获取内容更新对您而言是否重要

BT