1

我正在学习 Hartl 的 Ruby on Rails 教程,并被困在第 3.2 节,我需要使用以下命令运行 TDD:

$bundle exec rspec/requests/static_pages_spec.rb

我得到这个作为回报:

/home/Kelvin_Yu/rails_projects/sample_app/spec/requests/static_pages_spec.rb:1:in `require': /home/Kelvin_Yu/rails_projects/sample_app/spec/spec_helper.rb:2: syntax error, unexpected '.' (SyntaxError)
/home/Kelvin_Yu/rails_projects/sample_app/spec/spec_helper.rb:6: syntax error, unexpected '.'
    from /home/Kelvin_Yu/rails_projects/sample_app/spec/requests/static_pages_spec.rb:1:in `<top (required)>'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

所以我检查了我的“static_pages_spec.rb”文件,发现它与教程中的清单 3.9 相同(http://ruby.railstutorial.org/chapters/static-pages#sec-first_tests):

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
  end
end

所以这让我相信这不是错误的原因(如果这个假设不正确,请告诉我)并且我继续检查我的“spec_helper.rb”文件:

# This file is copied to spec/ when you run 'rails generate rspec:install'
.
.
.
RSpec.configure do |config|
  .
  .
  .
  config.include Capybara::DSL
end

这与教程中所示的相同(清单 3.10 @ http://ruby.railstutorial.org/chapters/static-pages#sec-first_tests)。

由于错误是“意外的”。(SyntaxError)”,我从第 2 行删除句点并重新运行 Rspec 命令。我得到了同样的错误,所以我从该文件中删除了所有句点,所以它现在是:

# This file is copied to spec/ when you run 'rails generate rspec:install'
RSpec.configure do |config|
  config.include Capybara::DSL
end

并得到一个不同的错误:

/home/Kelvin_Yu/rails_projects/sample_app/spec/spec_helper.rb:3:in `block in <top (required)>': uninitialized constant Capybara (NameError)
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core.rb:92:in `configure'
    from /home/Kelvin_Yu/rails_projects/sample_app/spec/spec_helper.rb:2:in `<top (required)>'
    from /home/Kelvin_Yu/rails_projects/sample_app/spec/requests/static_pages_spec.rb:1:in `require'
    from /home/Kelvin_Yu/rails_projects/sample_app/spec/requests/static_pages_spec.rb:1:in `<top (required)>'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

不确定这个错误“未初始化的常量 Capybara (NameError)”是什么意思。谁能帮助建议下一个最佳步骤是什么?

4

1 回答 1

2

作者示例中的那些点旨在用作省略号,表明那里有没有显示的文本(以使读者将注意力集中在相关位上)。

这更接近您的 spec_helper.rb 文件的外观:

# 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}

#def logger
#   Rails::logger
#end

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

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

如果您在删除多余的行后没有进行任何提交,请从您的 github 存储库中获取此文件的先前(完整)版本。

于 2013-07-19T18:15:49.617 回答