Rspec Test On Rails

今天,开始为自己的代码补充单元测试,用的是rails 里面的rspec,首先,
1.安装
gem install rspec

2.然后,生成
script/generate rspec_model performances
script/generate rspec_controller performances
:注:如果以前已经用script/generate model ..生成过模型或者控制器的,就选参数n,及不覆盖原来的代码。
3.fixture
 first_performance:
buskind: 102
score: 1.0

4.spec_model
require 'spec_helper'                                                                                                        

describe Performance do
fixtures :performances

before(:each) do
@valid_attributes = {
:buskind => 101,
:score => 1.0
}
end

it "should create a new instance given valid attributes" do
lambda do
Performance.create!(@valid_attributes)
end.should change(Performance,:count).by(1)
end

it "should be valid" do
performance = Performance.new @valid_attributes
performance.should be_valid
end
end

5.spec_controller
require 'spec_helper'

describe PerformancesController do
before(:each) do
login_as :admin
end

it "should save score " do
lambda do
xhr :post,:set_performance_score, :performance => {
:score => 100
},:id => performances(:first_performance)
end
response.should be_success
end
end

6.运行spec测试
spec spec/models/performance_spec.rb
spec spec/controllers/performances_controller.rb

7.另外可以通过tail -f log/test.log查看测试过程信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值