一. 前言
关于本篇文章介绍的安装步骤可以参考 《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的写到这里,详细内容可以参考原文,文本仅供一些参考。
分享到:
相关推荐
Rails::API 是 Rails 的精简版本,针对不需要使用完整 Rails 功能的开发者。 Rails::API 移除了 ActionView 和其他一些渲染功能,不关心Web前端的开发者可更容易、快速地开发应用程序,因此运行速度比正常的 Rails ...
init.d:Rails生产环境的:gear_selector:批处理脚本安装在Ubuntu Server上
使用Rails构建可伸缩和可维护API的最佳方法
1 准备工作 install nodejs # yum install httpd \httpd-devel \openssl-devel \zlib-devel \mysql-server \mysql-devel \gcc \gcc-c++ \...安装完成后按照提示将下面的内容添加到etc/httpd/httpd.conf LoadModule pa
gem "mongoid_rails_migrations" 如何使用 创建迁移 $ rails generate mongoid:migration 运行迁移: $ rails db:migrate $ rails db:migrate:down VERSION= $ rails db:migrate:up VERSION= $ rails db:rollback...
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
Windows7_Cygwin_Git_RVM_Ruby1.9.3_Rails3_MongoD B_Nginx_Unicorn_Rspec_Guard_Spork(2-Ruby on Rails3 安装配置
支持rails2.0以上版本的配置说明,文档从我自己的博客中整理出来,不愿意用资源分的朋友可以去我的博客看: http://wudihamagong.spaces.live.com/ 或者 http://blog.csdn.net/abcdefg0 ^_^
Ruby on Rails环境搭建(基于WinXP & RadRails) 博文链接:https://wangcheng.iteye.com/blog/113584
首先,请确保您已经创建了Rails应用程序 rails new APP_NAME 设置 确保您具有这些依赖项 yarn add jquery popper.js 确保Rails Gemfile包含以下宝石 # Gemfile gem 'autoprefixer-rails' gem 'simple_form' 在您的...
ruby on rails 环境搭建, 在windows 和Linux下的详细方法。
介绍如何在不同的系统上搭建Ruby on Rails开发环境,简单明了,带图
rails 配置文档 ror ruby on rails
gem修改Rails应用程序并配置: –安装支持Rails的RSpec gem –测试网页–的干净选择 –创建测试数据 –在Web浏览器中查看错误 –适用于需要JavaScript的测试该套件的宝石很流行用于测试Rails应用程序。 通常,开发...
在Mac下rails环境的搭建,主要是基于rubymine的开发,包含了一些错误的处理方法
factory_bot_rails ...添加factory_bot_rails到您的Gemfile的测试和开发组都: group :development , :test do gem 'factory_bot_rails' end 您可能希望将测试套件配置为包括factory_bot方法。 见。 自动工厂
在Rails 4.2及更高版本中,此gem将负责在Rails应用程序中sanitize HTML片段,即在sanitize , sanitize_css , strip_tags和strip_links方法中。 Rails Html Sanitizer仅适用于Rails应用程序。 如果您在非Rails应用...
这个gem将assigns给控制器测试的内容以及assert_template带回assigns控制器和集成测试的内容。 这些方法已中。 安装 将此行添加到您的应用程序的Gemfile中: gem 'rails-controller-testing' 然后执行: $ ...
SwaggerYard :: Rails SwaggerYard :: Rails gem是一个Rails引擎,旨在使用SwaggerYard解析您的Yardocs API控制器。 它将创建一个Swagger-UI投诉JSON,以通过安装... gem 'swagger-ui_rails2' , github : '3scal