Question

I am working through Hartl's Ruby on Rails tutorial and am stuck on Section 3.2 where I am required to run a TDD using the command:

$bundle exec rspec/requests/static_pages_spec.rb

and I get this in return:

/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'

So I check in my "static_pages_spec.rb" file and see that it is the same as in Listing 3.9 in the tutorial (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

So that makes me believe that this is not the cause of the error (please let me know if this assumption is incorrect) and I proceed to check my "spec_helper.rb" file:

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

This is the same as shown in the tutorial (Listing 3.10 @ http://ruby.railstutorial.org/chapters/static-pages#sec-first_tests).

Since the error is an "unexpected '.' (SyntaxError)", I remove the period from line 2 and rerun the Rspec command. I get the same error, so I remove all periods from that file so it is now:

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

and get a different error:

/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'

Not sure what this error "uninitialized constant Capybara (NameError)" means. Can anyone help advise what would be the next best steps?

Was it helpful?

Solution

Those dots in the author's examples are meant to serve as ellipses, indicating that there's text there that's not being shown (to keep the reader's focus on the relevant bits).

This is closer to what a your spec_helper.rb file should look like:

# 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

If you haven't made any commits since you removed the extra lines, grab the previous (complete) version of this file from your github repo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top