使用Minitest + Guard + Spork,每次运行时都会重新加载所有测试套件和支持文件

时间:2012-10-31 19:35:13

标签: ruby-on-rails-3 guard spork minitest

我遇到了一个问题,我的测试设置中的所有内容,包括Spork的prefork块,都在重新加载并在每次运行时调用。虽然这是一个新的项目,但是即使有一个新的项目,我也需要5-8秒的时间才能开始测试,即使这是一个新的项目也是如此,我甚至还有一个SSD。

在我意识到一切都在重新加载之前,我使用Jo Liss here描述的技术对其进行了分析,并且我已经包含了那些悬挂要求,但显然没有帮助,因为无论如何都要重新加载。但是,当我注意到这一点时:

  

装载套房   测试,测试/控制器,测试/控制器/管理,测试/工厂,测试/功能/管理,测试/帮手,测试/助手/管理,测试/集成,测试/模型,测试/支撑

我不明白为什么它会重新装载所有东西。这是我第一次尝试设置minitest + spork + guard(我本应该用rspec,叹气),所以我希望这是一个愚蠢的事情。我包含了一切,希望有人碰到它或者很容易发现我错过的东西。提前致谢!

我的档案包括:

  

test_helper.rb中

require 'spork'
Spork.prefork do
  #MiniTest::Rails.override_testunit!
  ENV["RAILS_ENV"] = "test"
  require File.expand_path('../../config/environment', __FILE__)
  require 'guard'
  require 'guard/interactors/readline'
  require 'guard/ui'
  require 'guard/interactors/coolline'
  require 'guard/interactor'
  require 'guard/dsl'
  require 'guard/notifiers/rb_notifu'
  require 'guard/notifier'
  require 'guard/minitest/notifier'
  require 'guard/minitest/runners/default_runner'
  Dir[File.expand_path('test/support/*.rb')].each { |file| require file }
  require "mocha"
  Spork.trap_method(Rails::Application, :reload_routes!)
  Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
end
  

Guardfile

guard 'bundler' do
  watch('Gemfile')
  # Uncomment next line if Gemfile contain `gemspec' command
  # watch(/^.+\.gemspec/)
end
guard 'spork', :wait => 65, :test_unit => false, :minitest => true, :minitest_env => { 'RAILS_ENV' => 'test' }, :bundler => true 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('test/test_helper.rb') { "test" }
end

guard 'minitest' do
  watch(%r|^test/test_helper\.rb|)         { "test" }
  watch(%r|^test/support/(.*)\.rb|)         { "test" }
  watch(%r{^test/.+_test\.rb$})
  watch(%r|^app/models/(.*)\.rb|)          {|m| ["test/models/#{m[1]}_test.rb", "test/integration/#{m[1]}_test.rb"] }
  watch(%r|^app/controllers/(.*)\.rb|)     {|m| ["test/controllers/#{m[1]}_test.rb", "test/integration/#{m[1]}_test.rb"] }
  watch(%r|^app/views/(.*)\.html|)         {|m| "test/integration/#{m[1]}_test.rb" }
  watch(%r|^app/objects/(.*)\.rb|)         {|m| "test/objects/#{m[1]}_object_test.rb" }
end

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)\.scss}) { |m| m[1] }
  watch(%r{(app/assets/.+\.js)\.coffee}) { |m| m[1] }
  watch(%r{config/locales/.+\.yml})
end
  

支持/ minitest.rb

#require "minitest/autorun"
require "minitest/rails"
require "minitest/spec"
require "minitest/pride"
require "minitest/rails/shoulda"
require "minitest/autorun"
require "capybara/rails"

class RequestSpec < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL
  include Rails.application.routes.url_helpers
end

MiniTest::Spec.register_spec_type /integration$/i, RequestSpec
#MiniTest::Spec.register_spec_type /object$/, MiniTest::Spec

class MiniTest::Rails::Controller
  include Devise::TestHelpers
end
  

支持/ turn.rb

require 'turn'
require 'turn/autorun'
require 'turn/colorize'
require 'turn/reporter'
require 'turn/reporters/pretty_reporter'
Turn.config do |c|
  c.natural = true
  c.ansi = true
  c.format = :pretty
end
  

的Gemfile

source 'https://rubygems.org'

# core
gem 'rails', '3.2.8'
gem 'thin'


# infrastructure
gem 'heroku'
gem 'foreman', :groups => [:development, :test]

# application monitoring
gem 'airbrake'
gem 'newrelic_rpm'


# view engine
gem 'haml', '>= 3.1.7'
gem 'haml-rails', '>= 0.3.5'
gem 'redcarpet' # markdown


# data persistence
gem 'mongoid', '>= 3.0.5'
gem 'redis'
gem 'redis-store'
gem 'redis-rails'


# email
gem 'sendgrid'
gem 'mailcatcher', :group => [:development]


# authentication and authorization
gem 'devise', '>= 2.1.2'
gem 'omniauth', '>= 1.0'
#gem 'authority'
gem 'cancan'
gem 'rolify', '>= 3.2.0'


# view helpers
gem 'bootstrap-generators', '~> 2.1', :group => [:development]
gem 'jquery-rails'
gem 'simple_form', '>= 2.0.2'
gem 'rails3-jquery-autocomplete'


# file attachments
gem 'rmagick'
gem 'fog'
gem 'carrierwave'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'


# url helpers
gem 'mongoid_slug'


# console tools
gem 'ansi',             :groups => [:development, :test]
gem 'rack-livereload',  :groups => [:development]
gem 'pry-rails',        :groups => [:development]
gem 'growl',            :groups => [:development, :test]
gem 'turn',             :groups => [:test]
gem 'awesome_print'
gem 'progress_bar',     :groups => [:test]


# supporting libraries
gem 'bson_ext'
gem 'nokogiri',                  :group => :test
gem 'ruby_parser', '>= 2.3.1',   :group => :test
gem 'rb-fsevent', '~> 0.9.1',    :group => :development


# assets
gem 'less-rails',                 :group => :assets
gem 'therubyracer', '>= 0.10.2',  :group => :assets
gem 'uglifier', '>= 1.0.3',       :group => :assets


# guard
gem 'guard',                     :group => :development
gem 'guard-livereload',          :group => :development
gem 'guard-spork',               :group => :development
gem 'guard-minitest',            :group => :development
gem 'guard-bundler',             :group => :development


# minitest
gem 'spork-minitest',           :group => :test
gem 'minitest-rails',           :group => :test
gem 'minitest-rails-shoulda',   :group => :test,  :git => 'git://github.com/rawongithub/minitest-rails-shoulda.git' 
gem 'capybara_minitest_spec',   :group => :test


# mocking
gem 'mocha', :group => :test


# http testing
gem 'vcr',      :group => :test
gem 'webmock',  :group => :test


# factories
gem 'factory_girl_rails'

1 个答案:

答案 0 :(得分:4)

Spork和Minitest似乎实际上没有按预期工作。为了解决这个问题,必须进行以下更改:

在Guardfile内部,:drb => true必须添加到minitest:

guard 'minitest', :drb => true do
  #...
end

然而,仅此一项就会破坏守卫/ minitest / spork,因为它会尝试强制提供当前不支持的-r -e选项。您将遇到以下错误:

Running: test/models/business_test.rb
Running tests with args ["-r", "/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/guard-minitest-0.5.0/lib/guard/minitest/runners/default_runner.rb", "-e", "::GUARD_NOTIFY=true", "test/test_helper.rb", "./test/models/business_test.rb"]...
Exception encountered: #<LoadError: cannot load such file -- -r>
backtrace:
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-minitest-0.0.3/lib/spork/test_framework/minitest.rb:13:in `block in run_tests'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-minitest-0.0.3/lib/spork/test_framework/minitest.rb:12:in `each'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-minitest-0.0.3/lib/spork/test_framework/minitest.rb:12:in `run_tests'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:13:in `block in run'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:21:in `block in initialize'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:18:in `fork'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:18:in `initialize'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/server.rb:48:in `run'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/drb/drb.rb:1548:in `perform_without_block'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/drb/drb.rb:1508:in `perform'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/drb/drb.rb:1586:in `block (2 levels) in main_loop'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/drb/drb.rb:1582:in `loop'
/Users/chance/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/drb/drb.rb:1582:in `block in main_loop'
Done.

幸运的是,有一个pull request for spork-minitest that fixes it

一旦宝石被指向回购,它就会很好地减少加载时间。它并不像node.js和mocha那样快,但它至少比rspec快。