- 浏览: 185625 次
- 性别:
- 来自: 武汉
-
文章分类
- 全部博客 (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 1184背景: 每次发布detail这边一般都要发布 ... -
ruby小体会
2012-11-30 19:03 01.=begin 创 建 者: 黄春霞 脚本功能:通过SV ... -
ruby regular
2012-11-30 17:17 9881. 一般规则: /a/ 匹配字符a。 ... -
ruby字符串的处理
2012-11-30 17:15 929最最常用的字符串处理函数 1.返回字符串的长度str. ... -
ruby中替换
2012-11-30 16:31 1402Ruby String http://si ... -
ruby中怎么用svn查看项目版本??
2012-11-28 10:14 7请问下各位大侠 在ruby中怎么引入svn的相关命令呢 有 ... -
Ruby中打印日志:Logger的使用
2011-06-13 15:38 15451.引入'logger' require ... -
Ruby对时间的处理函数
2011-06-13 15:38 1086Ruby对时间的处理函数1.当前时间 t = Time.ne ... -
Ruby类型转换函数及示例
2011-06-13 15:37 1119Ruby类型转换函数及示例1.数字转换成字符串to_s 示例 ... -
watir 关闭alert对话框
2011-06-13 15:36 952watir 关闭alert对话框alert真是一个很烦人的问题 ... -
watir 关闭打开的新窗口
2011-06-13 15:35 1243watir 关闭打开的新窗口在测试过程中,若页面通过链接或按钮 ... -
watir testUnit单元测试
2011-06-13 15:34 924watir testUnit单元测试 简单示例: r ... -
watir frames元素交互方式
2011-06-13 15:33 1197watir frames元素交互方式 frame: ... -
watir form内元素交互方式
2011-06-13 15:31 839watir form内元素交互方式 1.form提交按 ... -
watir text_field输入框交互方式
2011-06-13 15:30 1000<!-- 正文开始 --> watir t ... -
watir select元素交互方式
2011-06-13 15:29 942watir select元素交互方式 单选: <s ... -
watir checkbox复选框和radio单选交互方式
2011-06-13 15:28 1041watir checkbox复选框和radio单选交互方式 ... -
watir button交互方法
2011-06-13 15:28 943watir button交互方法 <input ... -
ruby和watir学习常用网站整理
2011-06-13 15:26 1377<!-- 正文开始 --> ruby和wati ... -
watir link链接元素交互方式
2011-06-13 15:25 826watir link链接元素交互方式 <a ...
相关推荐
《Ruby中的Hangman游戏:利用OOP和RSpec实践》 Ruby是一种强大且灵活的面向对象编程语言,它鼓励使用简洁的语法和清晰的代码结构。在这个项目中,我们通过实现一个经典的“Hangman”游戏来深入理解Ruby的面向对象...
6. **测试**:Rails强调TDD(测试驱动开发),提供了如RSpec和MiniTest等测试工具,确保代码的质量和稳定性。 在"Ruby Cookbook Source"中,你可能会找到关于以上概念的具体示例代码,帮助你理解如何在实际项目中...
列印发言 对于所有商品,Print Speak基本销售税适用10%的税率,但免税的书籍,食品和医疗产品除外 ...基本范例 基本例子 README.MD主文件中包含的基本示例的说明 class SomeRuby ; end 发展 签出仓库 git clone klue
编码测试 编码测试基本销售税适用于所有商品的10%的税率,但免税的书籍,食品和医疗产品除外...基本范例 基本例子 README.MD主文件中包含的基本示例的说明 class SomeRuby ; end 发展 签出仓库 git clone klueless-io/
Ruby-Httparty-Rspec-Json Matchers-Faker Essas ferramentas foram escolhidas por terem uma sintaxe defácilentendimento。 Possuemexecuçéesde testesrápidas,作为文档的简单范例,可以使当前的约会成为...
标题中的“国家FOIA项目”指的是“美国联邦信息自由法(Freedom of Information Act,简称FOIA)”,这是一项法律,允许...此外,它还可能提供了一种透明度和公众参与度的范例,鼓励公民参与政府事务并了解决策过程。
具有Ruby和JavaScript测试框架的经验,包括RSpec,Cucumber,Mocha,Chai等。提倡行为驱动开发。 全栈理解 在使用Ruby和JavaScript进行前端,后端,数据库和部署方面具有相关经验。 继续寻求和学习。 程式码范例 ...
"范例"一词表明这是一个用于教学或演示目的的示例代码库,可能是为了帮助初学者理解Ruby编程语言或者特定的Ruby库、框架的用法。标签"Ruby"进一步确认了这个压缩包的内容与Ruby编程语言直接相关。 Ruby是一种面向...
此外,Rails还强调测试驱动开发(TDD),通常包含一系列的测试文件,如RSpec或Test::Unit,用于确保代码的质量和功能的正确性。在"作品集-PE-v4"中,团队可能已经编写了测试用例,我们可以借此学习如何进行单元测试...