当前位置: 首页 > 工具软件 > rspec-2.x > 使用案例 >

rSpec 安装

贾越
2023-12-01
环境: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

测试通过,就表过,设计的行为达到要求
 类似资料: