关于本篇文章介绍的安装步骤可以参考 《Rails Tutorial - by example》 (英文)的3.2节。
如果还没有看过《搭建Rails测试环境:RSpec》 建议先看着这篇,文本将使用之前提到的环境。附件中提供了可测试本文操作的项目文件,建议在看直接下载该附件;
这个工具会在后台监控制定文件的改动,并且自动运行测试。例如,如果你改变了一个Controller,他会根这个Controller对应的测试。测试可以通过Growl进行反馈。
由于在每次运行Rspec时都需要重新加载Rails环境, Spork test server 将解决这个耗时的过程。他只加载一次Rails环境,并且维护一个pool of processes(这个地方不知道怎么描述,借用原文),为之后的测试提供Rails环境。Spork可以非常好的和AutoTest协同工作,这个可以大大提高测试效率;
sudo gem install autotest -v 4.4.6 sudo gem install autotest-rails-pure -v 4.1.2
如果你在 Mac OS 下想用Growl 来进行测试通知(推荐使用这种方式),需要安装autotest的 fsevent 和 growl 模块。当然也要确保已经安装了Growl软件(Linux和windows操作系统的通知方式可以参考原文)
sudo gem install autotest-fsevent -v 0.2.4 sudo gem install autotest-growl -v 0.2.9
vim ~/.autotest
加入如下配置,开启Growl通知:
require 'autotest/growl' require 'autotest/fsevent'
直接在Rails项目的根目录中输入 autotest 即可开启
在Gemfile的test group中加入 gem 'spork', '0.8.4', 然后运行 bundle install即可,完成Gemfile如下所示:
source 'http://rubygems.org' gem 'rails', '3.0.1' gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3' group :development do gem 'rspec-rails', '2.3.0' end group :test do gem 'rspec', '2.3.0' gem 'webrat', '0.7.2' gem 'spork', '0.8.4' end
如果安装spork失败,则自行使用gem安装:
sudo gem install spork
由于Spork现在的版本还不支持Rails3.x, 这理需要一些hack技巧来让Spork支持Rails3.x
引导Spork配置:
spork --bootstrap
修改spec/spec_helper.rb文件,添加如下 Spork.prefork代码块:
require 'rubygems' require 'spork' Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. ENV["RAILS_ENV"] ||= 'test' unless defined?(Rails) require File.dirname(__FILE__) + "/../config/environment" end require 'rspec/rails' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} Rspec.configure do |config| # == Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr config.mock_with :rspec config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, comment the following line or assign false # instead of true. config.use_transactional_fixtures = true ### Part of a Spork hack. See http://bit.ly/arY19y # Emulate initializer set_clear_dependencies_hook in # railties/lib/rails/application/bootstrap.rb ActiveSupport::Dependencies.clear end end Spork.each_run do end
如果对这个配置有疑问,可以参考附件中的对应文件
修改config/application.rb文件,修改后如下所示 :
require File.expand_path('../boot', __FILE__) require 'rails/all' # If you have a Gemfile, require the gems listed there, including any gems # you've limited to :test, :development, or :production. Bundler.require(:default, Rails.env) if defined?(Bundler) module SampleApp class Application < Rails::Application . . . ### Part of a Spork hack. See http://bit.ly/arY19y if Rails.env.test? initializer :after => :initialize_dependency_mechanism do # Work around initializer in railties/lib/rails/application/bootstrap.rb ActiveSupport::Dependencies.mechanism = :load end end end end
修改项目根目录中的rspec配置文件,添加--drb运行参数 (这个参数是启动对Spork的访问)
vim .rspec
修改后内容如下所示:
--colour --drb
ok~ 运行 spork 即可启动Spork
spork
AutoTest和Spork的写到这里,详细内容可以参考原文,文本仅供一些参考。