###Befor work
1)在Gemfile中添加如下代码
1 2 3 4 group :test do gem 'rspec' gem 'capybara' end
2)在Rakefile中添加如下代码
1 2 3 4 5 6 7 require 'rspec/core/rake_task' desc "Run specs" task :spec do RSpec::Core::RakeTask .new(:spec ) do |t | t.pattern = './spec/**/*_spec.rb' end end
3)在/ROOT/spec文件夹下的spec_helper.rb文件中添加如下代码
1 2 3 require 'rspec' require 'capybara/rspec' require 'rack/test'
做完以上步骤后就可以进行测试代码了
###Test
1)测试功能
在/ROOT/spec文件夹中添加lib_helper.rb
文件,里面中添加如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 require File.dirname(__FILE__ ) + '/spec_helper' $LOAD_PATH .unshift(File.dirname(__FILE__ ) + '/../lib' )require 'lib' describe Lib do before(:each ) do @lib = Lib.new end it "test name" do @lib .name = "test name" @lib .name.should == 'test name' end end
2)测试页面动作
在/ROOT/spec文件夹中添加root_helper.rb
文件,里面中添加如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 require File.dirname(__FILE__ ) + '/spec_helper' require File.join(File.dirname(__FILE__ ), '..' , 'app.rb' )Capybara.app = App.new Capybara.app = Sinatra::Application.new feature Root do context 'login' do it 'success' do visit '/login' within("#login" ) do fill_in 'name' , :with => 'admin' fill_in 'password' , :with => 'password' end click_button 'Login' current_path.should == '/' page.should have_content 'Login success' end end end
3)在命令行中运行rake spec
####Read more