rSpec 安装

环境:ruby 1.86 rails 2.34
安装:
1.
gem install rspec

2.一定要记得安这个
gem install rspec-rails

使得以下命令可用:

ruby script/generate rspec_controller
ruby script/generate rspec_model Organization name:string desription:string
ruby script/generate rspec_scaffold Organization name:string desription:string

3、在项目目录下运行
ruby script/generate rspec

将会生成相关的rake,使你的rake spec可用
4、在项目中用以下命令建模

ruby script/generate rspec_controller
ruby script/generate rspec_model User name:string email:string
ruby script/generate rspec_scaffold User name:string email:string

5、写测试代码和模型代码
Organization.rb
 
class Organization < ActiveRecord::Base
validates_presence_of :name, :message => "name can't be null"
end

Organization_spec.rb

require 'spec_helper'
require 'Organization'

describe Organization do
before(:each) do
@name = 'yxyԺ'
@desc = 'cbyxyԺ'
@valid_attributes = {
:name => @name,
:description => @desc
}
end

it "should create a new instance given valid attributes" do
o = Organization.create!(@valid_attributes)
o.name.to_s.should be(@name)
o.description.to_s.should be(@desc)
end

it "should name not be nil " do
Organization.create.errors.on(:name).should include("name can't be null")
Organization.create.should raise_error
end

end

6、用rake进行测试
rake
rake:all
rake:models
rake:views
rake:controller
.....

是运行相应目录下的所有有 _spec.rb文件
7、测试结果
>rake spec:models 
(in D:/Ruby/Apps/fee)
..

Finished in 0.328 seconds

2 examples, 0 failures

测试通过,就表过,设计的行为达到要求
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值