RAILS自动测试环境部署

gemfile

# source 'https://rubygems.org'
source 'http://ruby.taobao.org'
ruby '2.1.1'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.7'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
# gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
# gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails', '3.1.2'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks', '2.5.2'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
group :doc do
  gem 'sdoc', '0.4.0', require: false
end
group :development, :test do
  gem 'rspec-rails', '3.1.0'
  gem 'guard-rspec', '4.3.1'
  gem 'spork-rails', '4.0.0'
  gem 'guard-spork', github: "wudixiaotie/guard-spork"
  gem 'childprocess', '0.5.5'
  # Use debugger
  gem 'debugger', '1.6.8'
end

group :test do
  # gem 'selenium-webdriver', '2.44.0'
  gem 'poltergeist', '1.5.1'
  gem 'capybara', '2.4.4'
  gem 'minitest', '5.4.3'

  # Uncomment this line on OS X.
  # gem 'rspec-nc', '0.2.0'

  # Uncomment these lines on Linux.
  # gem 'libnotify', '0.8.4'

  # Uncomment these lines on Windows.
  # gem 'rb-notifu', '0.0.4'
  # gem 'win32console', '1.3.2'
  # gem 'wdm', '0.1.0'
end

2.

$ rails generate rspec:install
$ bundle exec guard init rspec

3.Guardfile中加入

require 'active_support/inflector'  

 4.

$ bundle exec spork --bootstrap

 5.将环境加载代码加入 Spork.prefork 代码块:spec/spec_helper.rb

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

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'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  # require 'rspec/autorun'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  
  # Checks for pending migrations before tests are run.
  # If you are not using ActiveRecord, you can remove this line.
  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
  
  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
  
    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    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, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true
  
    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false
  
    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = "random"
    config.include Capybara::DSL
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.

end

 6.Guard 和 Spork 协作

$ bundle exec guard init spork

 7.指定javascript driver为phantomjs取代selenium,在spec下建立support文件夹,里面添加文件capybara.rb

扫描二维码关注公众号,回复: 509421 查看本文章
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

 8.修改完之后,我们就可以通过 guard 命令同时启动 Guard 和 Spork 了:)

 9.最后别忘了把test文件夹删掉,否则会启动test::uiit

 10.如果是mac 还要在.rspec里加入

--format Nc

 来启用系统默认的通知。。

猜你喜欢

转载自wudixiaotie.iteye.com/blog/2156499