佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1530|回复: 7

[ROR] 用Rspec来测试Rails程序

[复制链接]
发表于 13-12-2007 11:56 PM | 显示全部楼层 |阅读模式
各位Rubyist,若是你还在用Rails本身自带的Test Suite的话,请停止再花时间来做无聊乏味的asserting。你也许听过TDD(Test-Driven Development) 和 BDD(Behavior Driven Development)这两种测试开发模式... 如果Rails的TestCase是TDD的话,那么RSpec就是BDD. 与其用assert,不如直接预测你的domain model的行为。

别说这么多,现来看看Rspec的一个User Model Spec的Syntax
  1. describe User, "with the name mike" do
  2.   fixtures :users,:roles,:rights,:resources,:attachments
  3.   
  4.   before(:each) do
  5.     @user = User.find :first, :conditions=>['name = ?','mike']
  6.   end

  7.   it "should be valid" do
  8.     @user.should be_valid
  9.   end
  10.   
  11.   it "should be able to authenticate" do
  12.     User.authenticate('mike','testing').should_not be_nil
  13.   end
  14.   
  15.   it "should have the attribute full name with Mike Staller" do
  16.     @user.full_name.should_not be_nil
  17.     @user.full_name.should == 'Mike Staller'
  18.   end
  19.   
  20.   it "should be administrator" do
  21.     @user.role.should eql(roles(:admin))
  22.   end
  23.   
  24.   it "should be able to access the user management" do
  25.     @user.role.should have(1).rights
  26.     right = @user.role.rights.first
  27.     right.controller.should == 'user_manager'
  28.     right.action.should == 'create'
  29.   end
  30.   
  31.   it "should have one attachment with a photo" do
  32.     @user.attachment.should_not be_nil
  33.     @user.attachment.resource.mime.should == 'image/jpeg'
  34.   end
  35. end
复制代码


即使你没学过Ruby,也应该看得出这个是在干什么的。若是还是觉得上面的测试有点乱,没关系,用rake spec:doc这个指令来看看summary:
  1. User with the name mike:
  2. - should be valid(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
  3. - should be able to authenticate(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
  4. - should have the attribute full name with Mike Staller (Spec::Rails::Example::ModelExampleGroup::Subclass_4)
  5. - should be administrator(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
  6. - should be able to access the user management(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
  7. - should have one attachment with a photo(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
复制代码


一目了然,而且出了Unit Test之外,还能够测试Rails的Controllers, Views, Helpers。

Rspec主页

Ryan Bates教你用RSpec测试Controller -- http://railscasts.com/episodes/71
回复

使用道具 举报


ADVERTISEMENT

发表于 14-12-2007 09:23 AM | 显示全部楼层
RUBY 很少人用吧 ?
回复

使用道具 举报

发表于 14-12-2007 11:21 AM | 显示全部楼层
不会啊。。。。
上次 有个 比赛 出席的人数很多


P/S:
这是很好的文章。谢谢分享。

[ 本帖最后由 tensaix2j 于 14-12-2007 11:24 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 15-12-2007 09:07 PM | 显示全部楼层
原帖由 tensaix2j 于 14-12-2007 11:21 AM 发表
不会啊。。。。
上次 有个 比赛 出席的人数很多


P/S:
这是很好的文章。谢谢分享。


虽然这里的很少有Ruby的踪迹... 但是还是想分享一下这个不可思议的语言...
至于Rails,不说这么多,试过就知道它的魅力。
回复

使用道具 举报

发表于 6-2-2008 05:54 PM | 显示全部楼层
原帖由 苦瓜汤 于 15-12-2007 09:07 PM 发表


虽然这里的很少有Ruby的踪迹... 但是还是想分享一下这个不可思议的语言...
至于Rails,不说这么多,试过就知道它的魅力。


很惭愧,用了RUBY RAILS 这么久,我都没试过做Testing。
一直以来都是直接Run程式后在Web Browser 做测试的。。。
回复

使用道具 举报

 楼主| 发表于 11-2-2008 09:49 PM | 显示全部楼层

回复 5# rainie520 的帖子

无可否认写test对于个人开发来说是一个繁琐的事情,因为很多时候许多系统的行为都已经在你脑海里有了一个雏形,所以直接把想法转为代码是一个比较简单的捷径;

但对于团队来说,TDD或BDD就比较重要了,毕竟每一个人有自己的想法,些出来的代码肯定会和要求的有出入。

不过,就长远来说,些test还是一个很好的习惯,对于未来维护系统或除虫都是一个很好的工具,因为当系统变得复杂的时候,用记忆来判断系统的行为就变得非常的不可靠。
回复

使用道具 举报

Follow Us
发表于 19-9-2014 08:59 PM | 显示全部楼层
RoR在malaysia多人用吗?
回复

使用道具 举报

发表于 20-9-2014 01:43 AM | 显示全部楼层
DEADslayerDARK 发表于 19-9-2014 08:59 PM
RoR在malaysia多人用吗?

RoR已经超红, 马来西亚可以看到不少公司在用的, 多数是startup
最近时常看到的myteksi也是用RoR + nodejs
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 26-8-2025 06:07 AM , Processed in 0.116485 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表