rspec --drb没有使用Spork在RSpec上工作

时间:2012-08-14 11:55:10

标签: ruby ruby-on-rails-3 railstutorial.org autotest spork

在完成Michael Hartl优秀的Rails教程之后,我决定尝试让Autotest和Spork加速我的测试。一切似乎安装正确,但当我来到最后的障碍(运行rspec与--drb标志)我收到以下错误:

$ 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,因为缺少任何其他想法。非常感谢到目前为止提供的所有帮助!

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

  

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

     

将RSpec :: Core :: CommandLine(从未正式声明为公共)合并到RSpec :: Core :: Runner中。 (迈伦马斯顿)

     

但spork依赖于此代码。

     

spork的github上已经存在一个问题,可以在后面的spork's 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