今天,开始为自己的代码补充单元测试,用的是rails 里面的rspec,首先,
1.安装
2.然后,生成
3.fixture
4.spec_model
5.spec_controller
6.运行spec测试
7.另外可以通过tail -f log/test.log查看测试过程信息
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查看测试过程信息