1

在完成 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,因为没有其他想法。非常感谢迄今为止提供的所有帮助!

4

3 回答 3

2

您使用的是旧版本的 Spork。您应该尝试升级到 v0.9.2 看看是否有帮助。

于 2012-08-16T16:07:16.970 回答
1

我知道我bundle exec rspec . --drb在另一个标签中运行 spork 后一直忘记。这消除了对我的任何奇怪的警告。也许这是给谷歌的。

于 2012-09-10T14:09:03.333 回答
0

请参阅 lx00st 对spork 0.9.2 和 rspec 3.0.0 = uninitialized constant RSpec::Core::CommandLine (NameError)的回答,引用:

原因是在 Rspec3 https://github.com/rspec/rspec-core/blob/master/Changelog.md中删除了 RSpec::Core::CommandLine

将 RSpec::Core::CommandLine(从未正式公开)合并到 RSpec::Core::Runner 中。(迈伦·马斯顿)

但是 spork 依赖于这个代码。

spork 的 github 上已经存在问题,可以在以下 spork 的 fork 中找到解决方案:

https://github.com/codecarson/spork/commit/38c79dcedb246daacbadb9f18d09f50cc837de51#diff-937afaa19ccfee172d722a05112a7c6fL6

一般 - 更换

::RSpec::Core::CommandLine.new(argv).run(stderr, stdout)

::RSpec::Core::Runner.run(argv,stderr, stdout)

在 soprks 源代码中

它对我来说效果很好(尽管我怀疑 spork gem 更新可能会删除源代码修复)。我刚刚用来自 github 的建议内容替换了内容 'spork/test_framework/rspec.rb':

require 'rspec/core/version'

class Spork::TestFramework::RSpec < Spork::TestFramework
  DEFAULT_PORT = 8989
  HELPER_FILE = File.join(Dir.pwd, "spec/spec_helper.rb")

  def run_tests(argv, stderr, stdout)
    if rspec1?
      ::Spec::Runner::CommandLine.run(
        ::Spec::Runner::OptionParser.parse(argv, stderr, stdout)
      )
    elsif rspec3?
      options = ::RSpec::Core::ConfigurationOptions.new(argv)
      ::RSpec::Core::Runner.new(options).run(stderr, stdout)
    else
      ::RSpec::Core::CommandLine.new(argv).run(stderr, stdout)
    end
  end

  def rspec3?
    return false if !defined?(::RSpec::Core::Version::STRING)
    ::RSpec::Core::Version::STRING =~ /^3\./
  end

  def rspec1?
    defined?(Spec) && !defined?(RSpec)
  end
end
于 2014-08-10T12:30:53.383 回答