ruby-on-rails – 每次Spork初始化导轨并慢慢运行

我需要帮助搞清楚为什么我的rspec测试运行缓慢.

我正在运行spork,guard和rspec.但是,似乎spork每次都重新初始化rails.我很难搞清楚如何调试此问题.我怀疑我使用的一些宝石是造成这种但没有证据的.

我认为它正在重装的原因是:

>在spork内部或外部运行需要相同的时间
>我得到了这个警告(与SOLR宝石相关),这是我跑步时得到的
rails服务器

运行所有规格

弃用警告:ActiveSupport :: Concern中的InstanceMethods模块将不再自动包含在内.请直接在Sunspot :: Rails :: SolrInstrumentation中定义实例方法. (从/home/dougt/replyinline/config/environment.rb:5调用)

这是我的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.
# 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'

  # 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
    config.include Devise::TestHelpers, :type => :view

    #https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec)
    config.extend ControllerMacros, :type => :controller
    config.extend ControllerMacros, :type => :helper
    config.extend ControllerMacros, :type => :view

  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
  #https://github.com/sporkrb/spork/issues/37
#   silence_warnings do
#      Dir["#{Rails.root}/app/models/**/*.rb"].each {|f| load f}
#  end
#  load "#{Rails.root}/config/routes.rb"
#  Dir["#{Rails.root}/app/**/*.rb"].each {|f| load f}

end

的Gemfile

source 'http://rubygems.org'

# 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'
#  gem "less-rails", "~> 2.1.4"
  gem 'twitter-bootstrap-rails',"~> 2.0.3"

end

group :test do
  # Pretty printed test output
  gem 'spork-rails'
  gem 'rspec-rails', '2.8'
  gem 'guard-spork'
  gem 'rspec'
  gem 'growl'
  gem 'rb-fsevent'
  gem 'guard-spork'

end

group :development do
  gem 'guard'
  gem 'guard-rspec'
  gem 'turn', '~> 0.8.3', :require => false
  gem 'webrat', '0.7.3'
  gem 'therubyracer'
  gem 'progress_bar'
  gem 'sunspot_solr'
  gem 'minitest'
  gem 'rspec-rails', '2.8'
end


gem 'rails', '3.2.2'
gem 'jquery-rails'
gem 'pg'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

gem 'execjs'
gem 'devise'
gem "cancan"
gem "oauth"
gem "oauth2"
gem "oauth-plugin", ">= 0.4.0.rc2"
gem "autotest"
gem "autotest-rails-pure"
gem "autotest-notification"
gem 'uuidtools'
gem 'json'
gem 'twitter'
gem 'ZenTest', '4.6.0'
gem 'mobylette'
gem 'diff-lcs'
gem 'twitter-text'
gem 'chronic',:git => 'git://github.com/tarr11/chronic.git'
gem 'sunspot_rails'
gem 'delayed_job_active_record', "~> 0.3.2"
gem 'delayed_task'
gem 'codemirror-rails', "~> 2.22"
gem 'thin'
gem "paperclip", "~> 2.0"
gem 'aws-sdk'
gem 'aws-s3'
gem 'newrelic_rpm'
gem 'airbrake'
gem 'factory_girl_rails', "~> 1.2"
gem 'diff_match_patch'
gem 'draper'
gem "email_veracity", "~> 0.6.0"

Guardfile

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

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') { :rspec }
  watch('test/test_helper.rb') { :test_unit }
  watch(%r{features/support/}) { :cucumber }
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{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml)$})                 { |m| "spec/#{m[1]}#{m[2]}_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('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

保护终端输出

dougt@dougt-ThinkPad-W520 ~/project $bundle exec guard
Guard uses NotifySend to send notifications.
Guard is now watching at '/home/dougt/project'
Starting Spork for RSpec
Using RSpec, Rails
Preloading Rails environment
Loading Spork.prefork block...
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in Sunspot::Rails::SolrInstrumentation instead. (called from <top (required)> at /home/dougt/project/config/environment.rb:5)
Spork is ready and listening on 8989!
Spork server for RSpec successfully started
Guard::RSpec is running, with RSpec 2!
Running all specs
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in Sunspot::Rails::SolrInstrumentation instead. (called from <top (required)> at /home/dougt/project/config/environment.rb:5)
/home/dougt/project/app/models/dropbox_token.rb:7: warning: already initialized constant ACCESS_TYPE
/home/dougt/project/app/models/dropbox_token.rb:9: warning: already initialized constant DROPBOX_SETTINGS
*.........**.....................***....***.....................***......*......................*

Pending:
  UserMailer add some examples to (or delete) /home/dougt/project/spec/mailers/user_mailer_spec.rb
    # No reason given
    # ./spec/mailers/user_mailer_spec.rb:4
  PersonNote add some examples to (or delete) /home/dougt/project/spec/models/person_note_spec.rb
    # No reason given
    # ./spec/models/person_note_spec.rb:4
  Task add some examples to (or delete) /home/dougt/project/spec/models/task_spec.rb
    # No reason given
    # ./spec/models/task_spec.rb:4
  Alert add some examples to (or delete) /home/dougt/project/spec/models/alert_spec.rb
    # No reason given
    # ./spec/models/alert_spec.rb:4
  Slide add some examples to (or delete) /home/dougt/project/spec/models/slide_spec.rb
    # No reason given
    # ./spec/models/slide_spec.rb:4
  BetaTester add some examples to (or delete) /home/dougt/project/spec/models/beta_tester_spec.rb
    # No reason given
    # ./spec/models/beta_tester_spec.rb:4
  SlideContent add some examples to (or delete) /home/dougt/project/spec/models/slide_content_spec.rb
    # No reason given
    # ./spec/models/slide_content_spec.rb:4
  SharedFile add some examples to (or delete) /home/dougt/project/spec/models/shared_file_spec.rb
    # No reason given
    # ./spec/models/shared_file_spec.rb:4
  FileActivity add some examples to (or delete) /home/dougt/project/spec/models/file_activity_spec.rb
    # No reason given
    # ./spec/models/file_activity_spec.rb:4
  EmailHelper add some examples to (or delete) /home/dougt/project/spec/helpers/email_helper_spec.rb
    # No reason given
    # ./spec/helpers/email_helper_spec.rb:14
  TodoLinesHelper add some examples to (or delete) /home/dougt/project/spec/helpers/todo_lines_helper_spec.rb
    # No reason given
    # ./spec/helpers/todo_lines_helper_spec.rb:14
  UserHelper add some examples to (or delete) /home/dougt/project/spec/helpers/user_helper_spec.rb
    # No reason given
    # ./spec/helpers/user_helper_spec.rb:14
  TasksHelper add some examples to (or delete) /home/dougt/project/spec/helpers/tasks_helper_spec.rb
    # No reason given
    # ./spec/helpers/tasks_helper_spec.rb:14
  todo_lines/show.html.erb add some examples to (or delete) /home/dougt/project/spec/views/todo_lines/show.html.erb_spec.rb
    # No reason given
    # ./spec/views/todo_lines/show.html.erb_spec.rb:4

Finished in 7.71 seconds
Running: spec/models/todo_file_spec.rb
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in Sunspot::Rails::SolrInstrumentation instead. (called from <top (required)> at /home/dougt/project/config/environment.rb:5)
/home/dougt/project/app/models/dropbox_token.rb:7: warning: already initialized constant ACCESS_TYPE
/home/dougt/project/app/models/dropbox_token.rb:9: warning: already initialized constant DROPBOX_SETTINGS
.....................

Finished in 3.05 seconds
21 examples, 0 failures
最佳答案
您可以对spork进行配置并使用结果来要求prefork块中的内容

http://www.opinionatedprogrammer.com/2011/02/profiling-spork-for-faster-start-up-time/

Devise以及可能还有其他gems调用reload也存在问题,可以通过在spec_helper prefork块中添加以下内容来捕获这些方法:

  ...
  ENV["RAILS_ENV"] ||= 'test'      
  # For Devise
  require "rails/application"
  Spork.trap_method(Rails::Application, :reload_routes!)
  Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
  ...

转载注明原文:ruby-on-rails – 每次Spork初始化导轨并慢慢运行 - 代码日志