在完成 Michael Hartl 的优秀 Rails 教程后,我决定尝试使用 Autotest 和 Spork 来加速我的测试。一切似乎都安装正确,但是当我遇到最后一个障碍(使用 --drb 标志运行 rspec)时,我收到以下错误:
$ rspec --drb spec/
*****************************************************************
DEPRECATION WARNING: you are using a deprecated constant that will
be removed from a future version of RSpec.
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/run_strategy/forking.rb:13:in `run'
* Spec is deprecated.
* RSpec is the new top-level module in RSpec-2
*****************************************************************
Exception encountered: #<NameError: uninitialized constant RSpec::Runner::CommandLine>
backtrace:
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/test_framework/rspec.rb:6:in `run_tests'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/run_strategy/forking.rb:13:in `run'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/forker.rb:21:in `initialize'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/forker.rb:18:in `fork'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/forker.rb:18:in `initialize'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/run_strategy/forking.rb:9:in `new'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/run_strategy/forking.rb:9:in `run'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/server.rb:48:in `run'
/usr/lib/ruby/1.8/drb/drb.rb:1558:in `__send__'
/usr/lib/ruby/1.8/drb/drb.rb:1558:in `perform_without_block'
/usr/lib/ruby/1.8/drb/drb.rb:1518:in `perform'
/usr/lib/ruby/1.8/drb/drb.rb:1592:in `main_loop'
/usr/lib/ruby/1.8/drb/drb.rb:1588:in `loop'
/usr/lib/ruby/1.8/drb/drb.rb:1588:in `main_loop'
/usr/lib/ruby/1.8/drb/drb.rb:1584:in `start'
/usr/lib/ruby/1.8/drb/drb.rb:1584:in `main_loop'
/usr/lib/ruby/1.8/drb/drb.rb:1433:in `run'
/usr/lib/ruby/1.8/drb/drb.rb:1430:in `start'
/usr/lib/ruby/1.8/drb/drb.rb:1430:in `run'
/usr/lib/ruby/1.8/drb/drb.rb:1350:in `initialize'
/usr/lib/ruby/1.8/drb/drb.rb:1630:in `new'
/usr/lib/ruby/1.8/drb/drb.rb:1630:in `start_service'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/server.rb:29:in `listen'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/lib/spork/server.rb:20:in `run'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/bin/../lib/spork/runner.rb:75:in `run'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/bin/../lib/spork/runner.rb:10:in `run'
/usr/lib/ruby/gems/1.8/gems/spork-0.9.0.rc8/bin/spork:10
/usr/lib/ruby/gems/1.8/bin/spork:23:in `load'
/usr/lib/ruby/gems/1.8/bin/spork:23
如果我只输入 $ rspec spec/ ,测试就可以完美运行,但是当然非常慢。所以看来问题可能出在我对 Spork 所做的事情上。我按照http://ruby.railstutorial.org/chapters/static-pages?version=3.0#sec:spork上的说明进行操作 (使用 v3.0 而不是 v3.2,因为这就是我开始使用的)。
请注意,我在 Windows 上运行 Cygwin,这意味着我使用的是 Ruby v1.8.7 而不是 1.9.2+。我不知道这是否会有所作为。
任何关于如何让 Spork 运行的想法(自动测试似乎很好)将不胜感激。
提前致谢!
抢
PS 我的 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
# 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
Spork.each_run do
# This code will be run each time you run your specs.
end
def test_sign_in(user)
controller.current_user = user
end
end
编辑 2:我更改了 spec_helper.rb 文件的结尾。我不认为我有额外的,正如建议的那样,但我认为他们在错误的地方,因为我的缩进很差。生成的文件(删除了所有注释)如下所示:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
end
end
Spork.each_run do
end
def test_sign_in(user)
controller.current_user = user
end
错误消息仍然相同。明天,如果有机会,我会尝试卸载 Ruby v1.8.7 并安装 v1.9.2 或 3,因为没有其他想法。非常感谢迄今为止提供的所有帮助!