安装使用rspec
一,安装ruby。
二,运行命令,安装rspec的gem包:
gem install rspec
会看到如下的结果:
Fetching: rspec-core-2.14.7.gem (100%)
Fetching: diff-lcs-1.2.5.gem (100%)
Fetching: rspec-expectations-2.14.5.gem (100%)
Fetching: rspec-mocks-2.14.6.gem (100%)
Fetching: rspec-2.14.1.gem (100%)
Successfully installed rspec-core-2.14.7
Successfully installed diff-lcs-1.2.5
Successfully installed rspec-expectations-2.14.5
Successfully installed rspec-mocks-2.14.6
Successfully installed rspec-2.14.1
5 gems installed
三,在纯ruby环境中使用rspec。
1,新建一个文件夹,随意命名为my_test。
2,在my_test下新建一个lib文件夹,lib文件夹下新建一个rb文件,例如bowling.rb。
3,在my_test下新建一个测试文件,命名为bowling_spec.rb,文件名一定要以_spec结尾。
4,在bowling.rb中写入如下代码:
# bowling.rb
class Bowling
def hit(pins)
end def score
0
end
end
5,在为bowling_spec.rb中写入如下测试代码:
# bowling_spec.rb
require 'bowling' describe Bowling, "#score" do
it "returns 0 for all gutter game" do
bowling = Bowling.new
20.times { bowling.hit(0) }
bowling.score.should eq(0)
end
end
6,运行命令,进行测试:
$ rspec bowling_spec.rb --format nested Bowling#score
returns 0 for all gutter game Finished in 0.007534 seconds 1 example, 0 failures
四,rails中使用rspec。
1,安装gem包,运行命令:
gem install rspec-rails
会看到如下的结果:
Fetching: i18n-0.6.9.gem (100%)
Fetching: minitest-4.7.5.gem (100%)
Fetching: atomic-1.1.14.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
Fetching: thread_safe-0.1.3.gem (100%)
Fetching: activesupport-4.0.3.gem (100%)
Fetching: builder-3.1.4.gem (100%)
Fetching: rack-1.5.2.gem (100%)
Fetching: actionpack-4.0.3.gem (100%)
Fetching: rspec-rails-2.14.1.gem (100%)
Successfully installed i18n-0.6.9
Successfully installed minitest-4.7.5
Successfully installed atomic-1.1.14
Successfully installed thread_safe-0.1.3
Successfully installed activesupport-4.0.3
Successfully installed builder-3.1.4
Successfully installed rack-1.5.2
Successfully installed actionpack-4.0.3
Successfully installed rspec-rails-2.14.1
9 gems installed
2,创建一个rails工程,进入工程目录,执行如下命令:
rails generate rspec:install
如果提示:Could not find generator rspec:install.
请看http://www.nujk.com/could-not-find-generator-rspec-install
这个时候,就会创建了spec的文件夹。
3,使用脚手架新建一个model文件:
rails generate model bowling
会看到如下执行结果:
invoke active_record
create db/migrate/20140224132433_create_bowlings.rb
create app/models/bowling.rb
invoke rspec
create spec/models/bowling_spec.rb
这个时候会发现,自动为bowlings.rb创建了对应的spec/models/bowling_spec.rb文件。
4,在bowlings.rb中添加一个方法:
class Bowling < ActiveRecord::Base
# attr_accessible :title, :body
def self.my_function()
return "this is function for test rspec"
end
end
5,在spec/models/bowling_spec.rb文件中为新加的方法写一个测试例子:
require 'spec_helper' describe Bowling do
#pending "add some examples to (or delete) #{__FILE__}"
it "shoud execute my_function" do
Bowling.my_function().should == "this is function for test rspec"
end
end
6,执行rspec测试
在rails工程目录下,执行:
rspec .\spec\models\bowling_spec.rb
这个时候会打印:
Finished in 0.02 seconds
1 example, 0 failures Randomized with seed 13492
结束:
以上仅仅是最基本的rspec安装步骤。对于rspec的详细请参见https://github.com/rspec/rspec-rails
安装使用rspec的更多相关文章
- rspec的一些常见用法
这里讲了如何安装rspec,安装使用rspec. 下面介绍一下rspec中常见的使用方法. 下面是一个最简单的测试用例,判断true是不是等于true,should_be是旧的用法,新用法推荐使用ex ...
- rspec学习02
元数据 RSpec-core存储元数据哈希每实例和组,其中包含他们的描述,声明的位置等等,这个hash控制很多RSpec核心的功能,包括输出格式化程序(访问描述和位置),和before,after钩子 ...
- 用rspec执行自动化测试用例
rspec是一款行为驱动开发(BDD)的工具,不过在这里用于测试,准确来说应该是测试驱动开发(TDD)吧.事实上我也没搞清楚.作为初学者不清楚就不清楚吧,以后会知道的.写博客无非就是写写学习笔记,不纠 ...
- Ruby(Selenium / Rspec)在Windows 8_64上安装步骤
1.首先需要下载RubyInstaller.exe程序(下载地址随便都能找到). 如:rubyinstaller-2.2.2-x64.exe 安装好Ruby后,需要更新Gems gem update ...
- ruby rspec安装
在rubymine里新建Rails application
- cucumber:环境安装
1.安装RubyInstallerhttp://rubyinstaller.org/downloads/注意:安装目录结构不要太深安装完成后在命令行运行: ruby –v 可以查看是否安装成功2.安装 ...
- CentOS 6.x安装Metasploit
现在开始安装Metasploit框架,前面的包安装成功之后,我们需要再安装一些Metasploit依赖的Ruby库,命令如下: gem install wirble pg sqlite3 msgpac ...
- rspec+rest-client测试第三方web service
如果你手工测试Restful 服务将会是一件非常单调乏味的事情.当然,目前有一些浏览器插件可以通过可视化的界面帮助你手工测试,例如postman.rest console,但是每次系统版本更新,你都需 ...
- Selenium WebDriver + Grid2 + RSpec之旅(四) ----通过RSpec编写测试用例
Selenium WebDriver + Grid2 + RSpec之旅(四) ----通过RSpec编写测试用例 自动化测试和手工测试一样,是要有测试用例,有检查点和测试结果的,接下来我们通过RSp ...
随机推荐
- 剑指Offer:面试题8——旋转数组的最小值(java实现)
题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入 一个递增排序的数组的一个旋转 输出 旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的 ...
- Orchard官方文档翻译(十) 管理Widgets
原文地址:http://docs.orchardproject.net/Documentation/Managing-widgets 想要查看文档目录请用力点击这里 最近想要学习了解orchard,但 ...
- Orchard官方文档翻译(四) 让Orchard在WebMatrix下工作
原文地址:http://docs.orchardproject.net/Documentation/Working-with-Orchard-in-WebMatrix 想要查看文档目录请用力点击这里 ...
- QTP 场景恢复– 函数调用
创建自动化测试是为了实现无人值守下运行,但也给开发人员带来一些问题.假如你离开办公室前启动测试,想要让它通宵运行.然而,由于不可预见的错误,您的测试会在某一点停止,中断了测试结果.因此QTP中引入场景 ...
- Flex4/AS3.0自定义VideoPlayer组件皮肤,实现Flash视频播放器
要求 必备知识 本文要求基本了解 Adobe Flex编程知识. 开发环境 Flash Builder4/Flash Player11 演示地址 演示地址 资料下载 Adobe Flash Pla ...
- Add, remove, shuffle and sort
To deal cards, we would like a method that removes a card from the deck and returns it. The list met ...
- No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- 使用VAssistX给文件和函数添加注释-2015.12.31
在Visual Studio使用VAssistX助手可以非常方便的给文件和函数添加注释,增加更多的记录信息,从而方便在时间久后,对代码阅读理解的提示,以及别人后续对代码的维护和BUG修改. 添加头文件 ...
- 修改VS解决方案及工程名,解决如何打开高/版本VS项目
对于VS2008等低版本与高版本VS之间的转换问题: 对照下面2个版本的不同点自由修改,切换到相应的版本文件(红字修改,灰色删除) ---------------------------------- ...
- C++记录debug信息的log类
取自:http://www.viksoe.dk/code/all_mfc.htm,里面有各种MFC常用的类 // LogFile.h: interface for the CLogFile class ...