`

rspec 范例

阅读更多

 原帖:http://elite28.github.com/2010/09/05/rspec%E6%A0%B7%E4%BE%8B.html

下面是一个ruby代码使用rspec进行行为驱动开发的样例:

mkdir rspec_tutorial
cd rspec_tutorial 
mate .

新建一个测试驱动文件

touch user_spec.rb

编辑内容:

describe User do
end     

这段测试代码的意思是,描述用户行为 在终端运行

spec user_spec.rb

这样会发生一个错误

./user_spec.rb:1: uninitialized constant User (NameError)

这个错误提示的意思是说没有初始化user对象,原因是我们还没有User类,所以现在要创建一个User类: 新建一个user.rb文件,内容:

class User
end

并且得包含到测试文件中(user_spec.rb)

require 'user'
describe User do
end

这次我们运行命令:

$ spec user_spec.rb

.

Finished in 0.010015 seconds

0 examples, 0 failures

这个输出告诉我们,现在还没有样例,所以我们可以这样在测试文件中定义:(user_spec.rb)

describe User do
  it "should be in any roles assigned to it" do
  end
end

it方法会返回一个example实例,因此it可以理解成“用户行为的一个样例” 这次我们在执行spec命令的时候加-f 参数,让结果按照某种格式来输出:

spec user_spec.rb -f specdoc     

User
- should be in any roles assigned to it

Finished in 0.022585 seconds

1 example, 0 failures

现在已经有了example了,然后是添加行为(user_spec.rb)

require 'user'
describe User do
  it "should be in any roles assigned to it" do
    user.should be_in_role("assigned role")
  end
end

加入的这行代码的意思是user应该能够胜任所有分配给他的角色。运行测试: $ spec user_spec.rb -f specdoc

User
- should be in any roles assigned to it (FAILED - 1)

1)
NameError in 'User should be in any roles assigned to it'
undefined local variable or method `user' for #<Spec::Example::ExampleGroup::Subclass_1:0x101547c28>
./user_spec.rb:4:

Finished in 0.008971 seconds

1 example, 1 failure

又出错了,是的,但在继续之前,让我们先仔细看看这段出错信息:

  • “ERROR -1)”告诉我们”should be in any roles assigned to it”这个样例出错了 * “1)”则为我们详细描述了这个错误,当样例很多时,你就会发现这个编号非常有用

还有一点需要注意:这段信息没有给出RSpec代码的backtrace,如果你需要它,可以通过–b选项来获取。

下面我们得在user_spec.rb中新建一个User对象:

require 'user'
describe User do
  it "should be in any roles assigned to it" do
    user = User.new
    user.should be_in_role("assigned role")
  end
end

再次执行测试: $ spec user_spec.rb -f specdoc

User
- should be in any roles assigned to it (FAILED - 1)

1)
NoMethodError in 'User should be in any roles assigned to it'
undefined method `in_role?' for #<User:0x101547778>
./user_spec.rb:5:

Finished in 0.008906 seconds

1 example, 1 failure

这次是因为User对象没有role_in?方法,修改user.rb:

class User
  def in_role?(role)
  end
end

再次执行测试: $ spec user_spec.rb -f specdoc

User
- should be in any roles assigned to it (FAILED - 1)

1)
'User should be in any roles assigned to it' FAILED
expected in_role?("assigned role") to return true, got nil
./user_spec.rb:5:

Finished in 0.008822 seconds

1 example, 1 failure

这段代码意思是调用in_role?方法返回nil,而不是true.所以通过测试很简单,直接在方法里面返回true即可:

class User
  def in_role?(role)
    true
  end
end

执行测试:

$ spec user_spec.rb -f specdoc

User
- should be in any roles assigned to it

Finished in 0.008493 seconds

1 example, 0 failures

但是到目前为止,我们在测试代码中还没分配给user对象角色,就对他进行了判断,所以应该在测试中加入(user_spec.rb):

require 'user'
describe User do
  it "should be in any roles assigned to it" do
    user = User.new
    user.assign_role("assigned role")
    user.should be_in_role("assigned role")
  end
end

再次执行测试代码不出意外会有下面的提示:

1)
NoMethodError in 'User should be in any roles assigned to it'
undefined method `assign_role' for #<User:0x101547408>
./user_spec.rb:5:

user.rb中没有assign_role的方法,所以添加一个(user.rb):

class User
  def assign_role(role)
  end

  def in_role?(role)
    true
  end
end

这次再执行测试,就可以通过了。 但是,现在,我们只是解决了“用户必须接受所有分配给他的角色”,但是还有一个问题就是”用户不应该接受没有分配给他的角色“。所以我们需要为用户行为再增加一个样例:

require 'user'
describe User do
  it "should be in any roles assigned to it" do
    user = User.new
    user.assign_role("assigned role")
    user.should be_in_role("assigned role")
  end

  it "不该属于没有分配给他的角色" do 
    user = User.new
    user.should_not be_in_role("unassigned role")
  end
end

执行测试:

$ spec user_spec.rb -f specdoc

User
- should be in any roles assigned to it
- 不该属于没有分配给他的角色 (FAILED - 1)

1)
'User 不该属于没有分配给他的角色' FAILED
expected in_role?("unassigned role") to return false, got true
./user_spec.rb:11:

Finished in 0.009124 seconds

2 examples, 1 failure

失败,用户接受了没有分配给他的角色,这时,我们需要对User.rb代码进行改动:

class User
  def assign_role(role) 
  end

  def in_role?(role)
    role == "assigned role"
  end
end

再次执行代码,没错了。 但是user.rb中有些重复,都是用了assigned role,因此要对user.rb的代码进行重构:

class User
  def assign_role(role) 
    @role = role
  end

  def in_role?(role)
    role == @role
  end
end

再来测试一下:

$ spec user_spec.rb -f specdoc

User
- should be in any roles assigned to it
- 不该属于没有分配给他的角色

Finished in 0.008864 seconds

2 examples, 0 failures
分享到:
评论

相关推荐

    rspec 入门者学习

    ### RSpec 入门者学习知识点详解 #### 一、RSpec 概述 RSpec 是一个流行的 Ruby 测试框架,主要用于行为驱动开发 (Behavior-Driven Development, BDD)。RSpec 的设计目的是让测试更加自然和易读,使得开发人员能够...

    the rspec book.pdf

    《RSpec Book》是一本专注于Rspec的权威指南,它详细阐述了如何使用Rspec这个强大的测试框架进行行为驱动开发(BDD)。Rspec是Ruby编程语言中的一个测试库,它使得编写可读性强、表达力丰富的测试代码成为可能。这...

    rspec测试.pdf

    《RSpec测试:行为驱动开发与RSpec、Cucumber及其他工具》 RSpec是一种用于Ruby语言的单元测试框架,它提倡一种称为“行为驱动开发”(Behavior Driven Development,BDD)的测试方式。RSpec允许开发者以自然语言的...

    RSpec 3.1中文版

    RSpec是Ruby语言开发的一款行为驱动开发(BDD)工具,它通过使用领域特定语言(DSL)来帮助开发人员编写测试用例。RSpec 3.1版本是RSpec框架的更新版,提供了更多的功能和更好的用户体验。Rails是一个用Ruby语言编写的...

    Pragmatic.The RSpec Book.2010.pdf

    #### 一、RSpec框架简介与特性 **RSpec** 是 Ruby 社区中最受欢迎的行为驱动开发(Behavior Driven Development, BDD)框架之一。它为开发者提供了一种灵活的方式来定义应用程序的行为,并通过简洁易读的语法来编写...

    rspec_api_documentation, 从RSpec自动生成API文档.zip

    rspec_api_documentation, 从RSpec自动生成API文档 RSpec Doc为你的Rails API生成漂亮的。查看一个示例文件。更改请查看维基以了解最新的更改。安装将rspec_api_documentation添加到你的文件gem 'rspec_a

    rspec測試工具書

    Rspec是一种行为驱动开发(Behavior-Driven Development,简称BDD)工具,它在软件测试领域被广泛使用。其目的是通过描述软件行为来提高开发人员与非技术团队成员之间的沟通效率。Rspec允许开发者编写一个可读性很强...

    使用RSpec 测试Rails 程序.pdf

    ### 使用RSpec 测试Rails 程序的知识点总结 #### 一、RSpec与Rails结合的基础概念 **RSpec**(RSpec is not a unit testing framework)是一种为Ruby编程语言设计的行为驱动开发(BDD)框架,而**Rails**是基于...

    jruby-1.5.5+OperaWatir+RSpec

    标题 "jruby-1.5.5+OperaWatir+RSpec" 暗示了这是一个关于使用 JRuby 1.5.5 版本、OperaWatir 和 RSpec 进行自动化测试的项目或者资源集合。现在,我们将深入探讨这三个关键组件以及它们在 IT 领域中的应用。 JRuby...

    RSpec.Essentials

    ### RSpec Essentials: Key Insights and Learning Points **RSpec Essentials** is an essential guide for developers looking to enhance their skills in testing Ruby applications using the RSpec framework...

    rspec-api-blueprint-formatter, 从RSpec测试自动生成API文档 !.zip

    rspec-api-blueprint-formatter, 从RSpec测试自动生成API文档 ! RSpec APIBlueprint格式化程序从RSpec测试自动生成API文档 !像这样it 'retrievs the patients medications' do retrieve_medications

    rspec-collection_matchers, 集合基数匹配器,从rspec期望中提取.zip

    rspec-collection_matchers, 集合基数匹配器,从rspec期望中提取 RSpec::CollectionMatchers RSpec::CollectionMatchers 让你在一个例子中表达一个对象集合的预期结果。expect(account.shopping_cart).to have_

    The.RSpec.Book

    ### 《RSpec 书籍》:行为驱动开发与RSpec、Cucumber等工具的深入探索 #### 知识点一:RSpec 概述 - **RSpec**(RSpec)是一种为Ruby编程语言设计的行为驱动开发(Behavior Driven Development, BDD)框架。它通过...

    rspec_junit_formatter, RSpec结果格式化为你的CI可以读取的JUnit.zip

    rspec_junit_formatter, RSpec结果格式化为你的CI可以读取的JUnit RSpec JUnit格式化程序 RSpec 2 & 3结果, Jenkins可以读取。 可能还有其他的CI服务。灵感来自于的工作,在的RSpec格式化程序在对 Reporter的失望...

    Ruby-RSpec和Minitest匹配器来预防N1查询问题

    Ruby提供了两种强大的测试工具,RSpec和Minitest,它们通过匹配器功能可以帮助我们预防这种问题的发生。 RSpec是Ruby中广泛使用的BDD(行为驱动开发)框架,它允许开发者以自然语言的方式编写测试。匹配器是RSpec的...

    BDD开发之rspec和cucumber

    ### BDD开发之rspec和cucumber #### 行为驱动开发(BDD)概览 行为驱动开发(Behavior-Driven Development, BDD)是一种软件开发方法论,它结合了敏捷开发的思想和技术,如测试驱动开发(TDD)和领域驱动设计(DDD)...

    Api-rspec_api_documentation.zip

    Api-rspec_api_documentation.zip,从rspecrspec api doc generator自动生成api文档,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库通信。通过提取实现并将数据放弃到...

    The Rspec Book -- BDD methodology

    ### 《RSpec Book》:行为驱动开发(BDD)方法论 #### 一、RSpec与行为驱动开发(BDD) **RSpec**是一种广泛应用于Ruby生态系统的测试框架,它支持一种称为**行为驱动开发(BDD)**的方法论。BDD不仅是一种测试方法,更...

Global site tag (gtag.js) - Google Analytics