• [Rails Tutorial] 自动化测试环境


    今天的这篇主要参考的Ruby-China的这篇帖子,基本是照搬…Orz

    1、 新建项目(不启用Test::Unit,不执行Bundle)

    $ rails new appname --skip-test-unit --skip-bundle
    

    2、 修改 Gemfile ,并 bundle install

    OS X 要安装 growl-notify ,是这里还是这里,就看你了。 liveload的浏览器扩展

    (Gemfile)download
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    
    require 'rbconfig'
    HOST_OS = RbConfig::CONFIG['host_os']
    source 'http://rubygems.org'
    gem 'rails', '3.2.3'
    # Gems used only for assets and not required
    # in production environments by default.
    group :assets do
      gem 'sass-rails',   '~> 3.2.3'
      gem 'coffee-rails', '~> 3.2.1'
      gem 'uglifier', '>= 1.0.3'
    end
    gem 'jquery-rails'
    gem 'rspec-rails' ,:group => [:development,:test]
    # jquery-ui
    # gem 'jquery-ui-rails'
    # bootstrap
    # gem 'anjlab-bootstrap-rails', :require => 'bootstrap-rails',:git => 'git://github.com/anjlab/bootstrap-rails.git'
    if HOST_OS =~ /linux/i
      gem 'therubyracer', '>= 0.8.2'
    end
    case HOST_OS
      when /darwin/i
        gem 'rb-fsevent', :group => :development
        gem 'growl', :group => :development
        gem 'guard-pow', :group => :development
      when /linux/i
        gem 'libnotify', :group => :development
        gem 'rb-inotify', :group => :development
      when /mswin|windows/i
        gem 'rb-fchange', :group => :development
        gem 'win32console', :group => :development
        gem 'rb-notifu', :group => :development
    end
    # Use unicorn as the web server
    # gem 'unicorn'
    # Deploy with Capistrano
    # gem 'capistrano'
    # To use debugger
    # gem 'ruby-debug19', :require => 'ruby-debug'
    group :test do
      # Pretty printed test output
      gem 'turn', :require => false
      gem 'factory_girl_rails' , "~> 1.1.0" ,:require => false
      gem 'database_cleaner', ">= 0.6.7"
      # gem 'mongoid-rspec', ">= 1.4.4"
      gem "capybara",">= 1.1.1"
      gem "launchy", ">= 2.0.5"
      gem 'guard-spork'
      gem 'spork', '~> 0.9.0'
      gem "guard-rspec"
      gem 'guard-livereload'
      gem 'guard-bundler'
    end
    

    3、 初始化 Rspec,Guard, Spork

    $ rails g rspec:install
    $ guard init
    

    $ bin/guard init rspec && bin/guard init spork

    修改 Guardfile

    (Guardfile)download
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    # A sample Guardfile
    # More info at https://github.com/guard/guard#readme
    guard 'livereload' do
      watch(%r{app/.+\.(erb|haml)})
      watch(%r{app/helpers/.+\.rb})
      watch(%r{(public/|app/assets).+\.(css|js|html)})
      watch(%r{(app/assets/.+\.css)\.s[ac]ss}) { |m| m[1] }
      watch(%r{(app/assets/.+\.js)\.coffee}) { |m| m[1] }
      watch(%r{config/locales/.+\.yml})
    end
    guard 'pow' do
      watch('.powrc')
      watch('.powenv')
      watch('.rvmrc')
      watch('Gemfile')
      watch('Gemfile.lock')
      watch('config/application.rb')
      watch('config/environment.rb')
      watch(%r{^config/environments/.*\.rb$})
      watch(%r{^config/initializers/.*\.rb$})
      watch(%r{^config/settings(\.local)?\.yml$})
    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('spec/spec_helper.rb')
    end
    guard 'rspec', :version => 2 ,:cli => "--drb",:notification => true 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{^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
    

    4、 修改 spechelper

    如果你没有使用activerecorder 一定要按注释修改文件.

    (spec_helper.rb)download
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    
    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("http://www.cnblogs.com/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
    

    5、执行 $ bundle exec spork --bootstrap

    6、以后只要运行 $ guard 就可以了

  • 相关阅读:
    LightOJ
    LightOJ
    51Nod 1021~1023 石子合并 (逐步加强版) 【dp】
    BZOJ1036 [ZJOI2008]树的统计Count 【树链剖分+线段树维护】
    51Nod 1677 treecnt 【树形dp+组合数学+逆元】
    逆元 【数学】
    51Nod 1705七星剑 【概率dp】
    BZOJ 1064 [Noi2008]假面舞会 【bfs】
    51 nod 1443 路径和树 【最短路径】
    BZOJ 1013 [JSOI2008]球形空间产生器sphere 【高斯消元】
  • 原文地址:https://www.cnblogs.com/rywx/p/2576911.html
Copyright © 2020-2023  润新知