【ArchSummit架构师峰会】探讨数据与人工智能相互驱动的关系>>> 了解详情
写点什么

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

  • 2007-10-31
  • 本文字数:2396 字

    阅读完需:约 8 分钟

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’<br></br>require ‘rbehave’<br></br>require ’spec’ # for "should" method<p>require ‘account’ # the actual application code</p><p>Story "transfer to cash account",</p><br></br>%(As a savings account holder<br></br>  I want to transfer money from my savings account<br></br>  So that I can get cash easily from an ATM) do<p>  Scenario "savings account is in credit" do</p><br></br>    Given "my savings account balance is", 100 do |balance|<br></br>      @savings_account = Account.new(balance)<br></br>    end<br></br>    Given "my cash account balance is", 10 do |balance|<br></br>      @cash_account = Account.new(balance)<br></br>    end<br></br>    When "I transfer", 20 do |amount|<br></br>      @savings_account.transfer_to(@cash_account, amount)<br></br>    end<br></br>    Then "my savings account balance should be", 80 do |expected_amount|<br></br>      @savings_account.balance.should == expected_amount<br></br>    end<br></br>    Then "my cash account balance should be", 30 do |expected_amount|<br></br>      @cash_account.balance.should == expected_amount<br></br>    end<br></br>  end<p>  Scenario "savings account is overdrawn" do</p><br></br>    Given "my savings account balance is", -20<br></br>    Given "my cash account balance is", 10<br></br>    When "I transfer", 20<br></br>    Then "my savings account balance should be", -20<br></br>    Then "my cash account balance should be", 10<br></br>  end<br></br>end<br></br>

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

class AccountSteps < Spec::Story::StepGroup<br></br>  steps do |define|<br></br>    define.given("my savings account balance is $balance") do |balance|<br></br>      @savings_account = Account.new(balance.to_f)<br></br>    end<p>    define.given("my cash account balance is $balance" do |balance|</p><br></br>      @cash_account = Account.new(balance.to_f)<br></br>    end<p>    define.then("my savings account balance should be $expected_amount" do |expected_amount|</p><br></br>      @savings_account.balance.should == expected_amount.to_f<br></br>    end<p>    define.then("my cash account balance should be $expected_amount" do |expected_amount|</p><br></br>      @cash_account.balance.should == expected_amount.to_f<br></br>    end<br></br>  end<br></br>end<p>steps = AccountSteps.new do |define|</p><br></br>  define.when("I transfer $amount") do |amount|<br></br>    @savings_account.transfer_to(@cash_account, amount.to_f)<br></br>  end<br></br>end<br></br>

在一个纯文本文件中按照那些步骤定义故事的行为: > Story: transfer to cash account<br></br>  As a savings account holder<br></br>  I want to transfer money from my savings account<br></br>  So that I can get cash easily from an ATM<p>  Scenario: savings account is in credit</p><br></br>    Given my savings account balance is 100<br></br>    And my cash account balance is 10<br></br>    When I transfer 20<br></br>    Then my savings account balance should be 80<br></br>    And my cash account balance should be 30<p>  Scenario: savings account is overdrawn</p><br></br>    Given my savings account balance is -20<br></br>    And my cash account balance is 10<br></br>    When I transfer 20<br></br>    Then my savings account balance should be -20<br></br>    And my cash account balance should be 10<br></br>

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

require 'spec'<br></br>require 'path/to/your/library/files'<br></br>require 'path/to/file/that/defines/account_steps.rb'<p># assumes the other story file is named the same as this file minus ".rb"</p><br></br>runner = Spec::Story::Runner::PlainTextStoryRunner.new(File.expand_path(__FILE__).gsub(".rb",""))<br></br>runner.steps << AccountSteps.new<br></br>runner.run<br></br>

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

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

2007-10-31 09:56901
用户头像

发布了 90 篇内容, 共 12.8 次阅读, 收获喜欢 10 次。

关注

评论

发布
暂无评论
发现更多内容

第一周学习总结

小何

netty系列之:让TLS支持http2

程序那些事

Netty 网络协议 HTTP 程序那些事 http2

03-简单工厂模式

千羽的编程时光

设计模式

05-抽象工厂模式

千羽的编程时光

设计模式

架构训练营模块一作业

小何

架构实战营

Leetcode 题目解析:211. 添加与搜索单词 - 数据结构设计

程序员架构进阶

LeetCode Trie 算法题 10月月更

阿里云盘:慌了....

Jackpop

广角-聊聊Underlay

Lance

容器 云原生 Underlay

docker 系列:底层知识

yuexin_tech

Docker

应区块链而生的元宇宙

CECBC

【LeetCode】最小操作次数使数组元素相等Java题解

Albert

算法 LeetCode 10月月更

Prometheus 查询操作符(三) 聚合运算符

耳东@Erdong

Prometheus 10月月更

谈 C++17 里的 Command 模式

hedzr

设计模式 命令模式 Design Patterns c++17 Command Pattern

砀山“区块链+农业” 推动产业“提档升级”

CECBC

在线分数约分计算器

入门小站

工具

【Flutter 专题】24 易忽略的【小而巧】的技术点汇总 (三)

阿策小和尚

Flutter 小菜 0 基础学习 Flutter Android 小菜鸟 10月月更

linux之tar使用技巧

入门小站

Linux

Github爆款!Aura v2.0.0正式版来了…

Jackpop

【设计模式】第七篇 - 建造者模式-麦当劳套餐

Brave

设计模式 建造者模式 10月月更

java.lang.OutOfMemoryError:GC overhead limit exceeded

看山

Java OOM 10月月更

JavaAgent查看动态生成类的源码

长河

Spinnaker:云原生多云环境持续部署的未来

博文视点Broadview

行动造就未来,区块链给农产品用上“数字身份证”

CECBC

一场穿越千年的智能矿山“梦游记”

脑极体

Agora 教程丨如何实现15mins自主搭建一个教育平台?

声网

人工智能 大数据

新手 Gopher 如何写出更健壮的 Go 代码

baiyutang

golang 10月月更

Vue进阶(幺肆贰):CSS-静态定位,相对定位,绝对定位,固定定位的用法和区别详解

No Silver Bullet

Vue 元素定位 10月月更

04-工厂方法模式

千羽的编程时光

设计模式

这款手机浏览器,简直开挂了....

Jackpop

元宇宙:区块链时代的代名词

CECBC

阿里IM技术分享(五):闲鱼亿级IM消息系统的及时性优化实践

JackJiang

架构设计 即时通讯 IM

RSpec增加了众所翘首以待的RBehave功能以供集成测试所需_研发效能_Sean Miller_InfoQ精选文章