- 浏览: 177378 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (150)
- mysql (8)
- ssh (2)
- 总结 (14)
- 个人心扉 (5)
- 网摘 (14)
- 其它 (2)
- javascript (0)
- ajax (3)
- jquery (0)
- 正则表达式 (2)
- 我们的项目 (1)
- Java代理(静态和动态) (2)
- java (19)
- jsp (5)
- servlet (1)
- uml (0)
- java 细节常识 (1)
- 学习网址哈哈 (2)
- python (5)
- Wearing other`s shoes (6)
- jdbc (1)
- JMeter (18)
- 测试 (9)
- linux操作 (2)
- couchdb (0)
- ruby (20)
- QTP资源地址 (1)
- rspec 范例 (1)
- LR (3)
- spring (1)
- 自动化 (2)
- 无线测试相关 (1)
最新评论
-
1336224635:
...
EJB -
guanzhongdaoke54007:
很好很强大~[color=blue][/color][size ...
ResultSet -
enum:
thank's so mauch.
font color to ...
java生成登录验证码的方法 -
harbey:
文章很好,很受用!
转帖Jmeter中的几个重要测试指标释义 -
xiangguanglei:
非常感谢!这个问题苦恼了我很久。
关于ruby + watir
原帖: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
发表评论
-
结合ruby写的一个校验工具
2013-01-25 16:15 931背景: 每次发布detail这边一般都要发布 ... -
ruby小体会
2012-11-30 19:03 01.=begin 创 建 者: 黄春霞 脚本功能:通过SV ... -
ruby regular
2012-11-30 17:17 9231. 一般规则: /a/ 匹配字符a。 ... -
ruby字符串的处理
2012-11-30 17:15 888最最常用的字符串处理函数 1.返回字符串的长度str. ... -
ruby中替换
2012-11-30 16:31 1351Ruby String http://si ... -
ruby中怎么用svn查看项目版本??
2012-11-28 10:14 7请问下各位大侠 在ruby中怎么引入svn的相关命令呢 有 ... -
Ruby中打印日志:Logger的使用
2011-06-13 15:38 14711.引入'logger' require ... -
Ruby对时间的处理函数
2011-06-13 15:38 1018Ruby对时间的处理函数1.当前时间 t = Time.ne ... -
Ruby类型转换函数及示例
2011-06-13 15:37 1074Ruby类型转换函数及示例1.数字转换成字符串to_s 示例 ... -
watir 关闭alert对话框
2011-06-13 15:36 908watir 关闭alert对话框alert真是一个很烦人的问题 ... -
watir 关闭打开的新窗口
2011-06-13 15:35 1181watir 关闭打开的新窗口在测试过程中,若页面通过链接或按钮 ... -
watir testUnit单元测试
2011-06-13 15:34 849watir testUnit单元测试 简单示例: r ... -
watir frames元素交互方式
2011-06-13 15:33 1149watir frames元素交互方式 frame: ... -
watir form内元素交互方式
2011-06-13 15:31 798watir form内元素交互方式 1.form提交按 ... -
watir text_field输入框交互方式
2011-06-13 15:30 918<!-- 正文开始 --> watir t ... -
watir select元素交互方式
2011-06-13 15:29 904watir select元素交互方式 单选: <s ... -
watir checkbox复选框和radio单选交互方式
2011-06-13 15:28 999watir checkbox复选框和radio单选交互方式 ... -
watir button交互方法
2011-06-13 15:28 886watir button交互方法 <input ... -
ruby和watir学习常用网站整理
2011-06-13 15:26 1326<!-- 正文开始 --> ruby和wati ... -
watir link链接元素交互方式
2011-06-13 15:25 779watir link链接元素交互方式 <a ...
相关推荐
原版The RSpec Book并附有源码
rspec_api_documentation, 从RSpec自动生成API文档 RSpec Doc为你的Rails API生成漂亮的。查看一个示例文件。更改请查看维基以了解最新的更改。安装将rspec_api_documentation添加到你的文件gem 'rspec_a
rspec出入门者学习理解,介绍的非常的详细、清楚; 欢迎下载学习~
rspec 下载请注意:英文文档
rspec-api-blueprint-formatter, 从RSpec测试自动生成API文档 ! RSpec APIBlueprint格式化程序从RSpec测试自动生成API文档 !像这样it 'retrievs the patients medications' do retrieve_medications
jruby-1.5.5+OperaWatir+RSpec
使用RSpec 测试Rails 程序.pdf
rspec-collection_matchers, 集合基数匹配器,从rspec期望中提取 RSpec::CollectionMatchers RSpec::CollectionMatchers 让你在一个例子中表达一个对象集合的预期结果。expect(account.shopping_cart).to have_
使用RSpec测试Rails程序。本书基于Rails 4.1,使用完整应用 来演示TDD的流程。
rspec 是目前bdd測試的使用工具,很適合描述測試的內容
rspec_junit_formatter, RSpec结果格式化为你的CI可以读取的JUnit RSpec JUnit格式化程序 RSpec 2 & 3结果, Jenkins可以读取。 可能还有其他的CI服务。灵感来自于的工作,在的RSpec格式化程序在对 Reporter的失望...
RSpec.Essentials 英文
ruby(rails)单元测试相关的gem,rspec、cucumeber。BDD
RSpec和Minitest匹配器来预防N 1查询问题
Api-rspec_api_documentation.zip,从rspecrspec api doc generator自动生成api文档,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库通信。通过提取实现并将数据放弃到...
在他们下载的chm始终打不开或有问题。 ... 使用Cucumber+Rspec玩转BDD(1)——用户注册 使用Cucumber+Rspec玩转BDD(2)——邮件激活 ...使用Cucumber+Rspec玩转BDD(3)——用户...使用Cucumber+Rspec玩转BDD(7)——测试重构
db-query-matchers, 用于数据库查询的RSpec匹配器 db-query-matchers 用于数据库查询的RSpec匹配器。安装在你的应用程序中添加这一行,最好在你的test 组中:gem 'db-query-matchers'然后执行:bundle
rspec_demo Rspec演示
rspec-2.x 是一个 meta-gem,依赖于其他组件:rspec-core, rspec-expectations 和 rspec-mocks 。每个组件都可以使用 gem 命令独立安装和运行。 标签:rspec