查看: 1529|回复: 7
|
[ROR] 用Rspec来测试Rails程序
[复制链接]
|
|
各位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
- describe User, "with the name mike" do
- fixtures :users,:roles,:rights,:resources,:attachments
-
- before(:each) do
- @user = User.find :first, :conditions=>['name = ?','mike']
- end
- it "should be valid" do
- @user.should be_valid
- end
-
- it "should be able to authenticate" do
- User.authenticate('mike','testing').should_not be_nil
- end
-
- it "should have the attribute full name with Mike Staller" do
- @user.full_name.should_not be_nil
- @user.full_name.should == 'Mike Staller'
- end
-
- it "should be administrator" do
- @user.role.should eql(roles(:admin))
- end
-
- it "should be able to access the user management" do
- @user.role.should have(1).rights
- right = @user.role.rights.first
- right.controller.should == 'user_manager'
- right.action.should == 'create'
- end
-
- it "should have one attachment with a photo" do
- @user.attachment.should_not be_nil
- @user.attachment.resource.mime.should == 'image/jpeg'
- end
- end
复制代码
即使你没学过Ruby,也应该看得出这个是在干什么的。若是还是觉得上面的测试有点乱,没关系,用rake spec:doc这个指令来看看summary:
- User with the name mike:
- - should be valid(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
- - should be able to authenticate(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
- - should have the attribute full name with Mike Staller (Spec::Rails::Example::ModelExampleGroup::Subclass_4)
- - should be administrator(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
- - should be able to access the user management(Spec::Rails::Example::ModelExampleGroup::Subclass_4)
- - 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 |
|
|
|
|
|
|
|
发表于 14-12-2007 09:23 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 14-12-2007 11:21 AM
|
显示全部楼层
不会啊。。。。
上次 有个 比赛 出席的人数很多
P/S:
这是很好的文章。谢谢分享。
[ 本帖最后由 tensaix2j 于 14-12-2007 11:24 AM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 15-12-2007 09:07 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 6-2-2008 05:54 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 11-2-2008 09:49 PM
|
显示全部楼层
回复 5# rainie520 的帖子
无可否认写test对于个人开发来说是一个繁琐的事情,因为很多时候许多系统的行为都已经在你脑海里有了一个雏形,所以直接把想法转为代码是一个比较简单的捷径;
但对于团队来说,TDD或BDD就比较重要了,毕竟每一个人有自己的想法,些出来的代码肯定会和要求的有出入。
不过,就长远来说,些test还是一个很好的习惯,对于未来维护系统或除虫都是一个很好的工具,因为当系统变得复杂的时候,用记忆来判断系统的行为就变得非常的不可靠。 |
|
|
|
|
|
|
|
发表于 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
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|