搭建Rails测试环境2: Autotest和Spork的安装和配置
2016-07-29 00:00
706 查看
一. 前言
关于本篇文章介绍的安装步骤可以参考 《Rails Tutorial - by example》 (英文)的3.2节。如果还没有看过《搭建Rails测试环境:RSpec》 建议先看着这篇,文本将使用之前提到的环境。附件中提供了可测试本文操作的项目文件,建议在看直接下载该附件;
二. 测试工具介绍:
1. AutoTest:
这个工具会在后台监控制定文件的改动,并且自动运行测试。例如,如果你改变了一个Controller,他会根这个Controller对应的测试。测试可以通过Growl进行反馈。2. Spork:
由于在每次运行Rspec时都需要重新加载Rails环境, Spork test server 将解决这个耗时的过程。他只加载一次Rails环境,并且维护一个pool of processes(这个地方不知道怎么描述,借用原文),为之后的测试提供Rails环境。Spork可以非常好的和AutoTest协同工作,这个可以大大提高测试效率;三. AutoTest安装配置:
1. 安装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
2. 配置AutoTest:
vim ~/.autotest
加入如下配置,开启Growl通知:
require 'autotest/growl' require 'autotest/fsevent'
3. 如何使用:
直接在Rails项目的根目录中输入 autotest 即可开启四. Spork的安装和使用:
1. 安装Spork:
在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
2. 配置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的写到这里,详细内容可以参考原文,文本仅供一些参考。
相关文章推荐
- VIM编译器显示TAB和TAB转换为空格
- 菜鸟说给菜鸟听之VIM--入门篇(1)
- cygwin下vim方向键不好用的解决方案
- 安装使用Ubuntu之vim必须知道的细节
- 红宝石(Ruby)史话
- Ruby on Rails所构建的应用程序基本目录结构总结
- Ruby配置rspec和RestClient来检测服务器
- Ruby on Rails中jquery_ujs组件拖慢速度的问题解决
- Ruby on Rails框架程序连接MongoDB的教程
- 浅谈Rails 4 中Strong Parameters机制
- Ruby on Rails网站项目构建简单指南
- Ruby on Rails中的ActiveResource使用详解
- 关于Ruby on Rails视图编写的一些建议
- Ruby on Rails中的ActiveRecord编程指南
- 对优化Ruby on Rails性能的一些办法的探究
- 简单对比分析Ruby on Rails 和 Laravel
- Ruby学习笔记二帮助生成Vim添加代码头的代码
- 在 Ubuntu 12.04 Server 上安装部署 Ruby on Rails 应用
- Ruby on Rails下的图像处理入门教程
- 详解Ruby on Rails中的Cucumber使用