Rails 3.2.3 使用 Spork + Guard + RSpec + Capybara + livereload

7 篇文章 0 订阅
3 篇文章 0 订阅
参考如下文章做了一片,挺好的,此处做个备份。
http://ruby-china.org/topics/2990
  1.   init project
    rails new appname --skip-test-unit --skip-bundle

  2. update Gemfile
    source 'http://ruby.taobao.org/'
    
    gem 'rails', '3.2.3'
    
    group :development do
      gem 'sqlite3', '1.3.5'
      gem 'rspec-rails', '2.9.0'
      gem 'guard-rspec', '0.5.5'
    end
    
    group :assets do
      gem 'sass-rails',   '3.2.4'
      gem 'coffee-rails', '3.2.2'
      gem 'uglifier', '1.2.3'
    end
    
    gem 'jquery-rails', '2.0.0'
    
    group :test do
      gem 'rspec-rails', '2.9.0'
      gem 'capybara', '1.1.2'
      gem 'rb-inotify', '0.8.8'
      gem 'libnotify', '0.5.9'
      gem 'guard-spork', '0.3.2'
      gem  'guard-rspec', '1.2.0'
      gem 'guard-livereload'
      gem 'spork', '0.9.0'
    end

  3. 安装gem
    bundle install 


  4. init rspec
    rails g rspec:install

  5. init spork , init guard
    bundle exec init rspec && bundle exec  init spork

  6. 初始化guard
    bundle exec guard init

  7. 修改gardfile
    # A sample Guardfile
    # More info at https://github.com/guard/guard#readme
    require 'active_support/core_ext'
    
    guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' } do
      watch('config/application.rb')
      watch('config/environment.rb')
      watch(%r{^config/environments/.+\.rb$})
      watch(%r{^config/initializers/.+\.rb$})
      watch('Gemfile')
      watch('Gemfile.lock')
      watch('spec/spec_helper.rb')
      watch('test/test_helper.rb')
      watch('spec/support/')
    end
    
    guard 'rspec', :version => 2, :all_after_pass => false, :cli => '--drb' do
      watch(%r{^spec/.+_spec\.rb$})
      watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
      watch('spec/spec_helper.rb') { "spec" }
    
      watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
        ["spec/routing/#{m[1]}_routing_spec.rb",
         "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
         "spec/acceptance/#{m[1]}_spec.rb",
         (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
                           "spec/requests/#{m[1].singularize}_pages_spec.rb")]
      end
      watch(%r{^app/views/(.+)/}) do |m|
        "spec/requests/#{m[1].singularize}_pages_spec.rb"
      end
    
      # Rails example
      watch(%r{^spec/.+_spec\.rb$})
      watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
      watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
      watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
      watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
      watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
      watch('spec/spec_helper.rb') { "spec" }
      watch('config/routes.rb') { "spec/routing" }
      watch('app/controllers/application_controller.rb') { "spec/controllers" }
      # Capybara request specs
      watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
    end
    
    
    guard 'rspec', :version => 2 do
      watch(%r{^spec/.+_spec\.rb$})
      watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
      watch('spec/spec_helper.rb') { "spec" }
    
      # Rails example
      watch(%r{^spec/.+_spec\.rb$})
      watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
      watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
      watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
      watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
      watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
      watch('spec/spec_helper.rb') { "spec" }
      watch('config/routes.rb') { "spec/routing" }
      watch('app/controllers/application_controller.rb') { "spec/controllers" }
      # Capybara request specs
      watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
    end
    
    
    guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
      watch('config/application.rb')
      watch('config/environment.rb')
      watch(%r{^config/environments/.+\.rb$})
      watch(%r{^config/initializers/.+\.rb$})
      watch('Gemfile')
      watch('Gemfile.lock')
      watch('spec/spec_helper.rb')
      watch('test/test_helper.rb')
    end

  8. 修改.rspec,增加
    --drb

  9. 修改spec/spec_helper.rb  
    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.
      # This file is copied to spec/ when you run 'rails generate rspec:install'
      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}
    
      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
    
        # 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
      end
    end
    
    Spork.each_run do
      # This code will be run each time you run your specs.
    
    end

  10. 运行
    bundle exec spork --bootstrap

  11. 启动guard
    bundle exec guard

  12. 使用livereload自动刷新页面Gemfile
    gem 'rack-livereload'
    gem 'guard-livereload'

    bundle install

    bundle exec guard init livereload


    修改development.rb文件
    config.middleware.insert_before(Rack::Lock, Rack::LiveReload)

  13. 补充说明 spork ######################################################
    spork会缓存 model 和 controller ,每次对model的修改都需要重启 服务器, 非常麻烦。所以,修改spec_helper.rb文件, 不需要每次都重启服务器。

    Spork.each_run do
    
      # Reload all model files when run each spec
      # otherwise there might be out-of-date testing
      require 'rspec/rails'
      Dir["#{Rails.root}/app/controllers//*.rb"].each do |controller|
        load controller
      end 
    
      Dir["#{Rails.root}/app/models//*.rb"].each do |model|
        load model
      end 
    
      FactoryGirl.reload
    end
    

    还有,设置 config/enviroments/test.rb文件
      config.cache_classes = ENV['DRB'] == 'true' ? false : true
    





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值