Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec error during Rails Tutorial : undefined local variable or method `static_pages_index_path'

I'm followind the Rails Tutorial but have a problem in section 3.2.1, just before figure 3.6. When running

$ bundle exec rspec spec/requests/static_pages_spec.rb

I get failure

Failures:
  1) StaticPages GET /static_pages works! (now write some real specs)
     Failure/Error: get static_pages_index_path
     NameError:
       undefined local variable or method `static_pages_index_path' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe7592b33b8>
     # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'    
Finished in 0.00454 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # StaticPages GET /static_pages works! (now write some real specs)

here are the files :

spec/requests/static_pages_spec.rb

require 'spec_helper'
describe "Static pages" do
  describe "Home page" do
    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      page.should have_content('Sample App')
    end
  end
end

app/controllers/static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
  end
  def help
  end
end

app/views/static_pages/home.html.erb

<h1>Sample App</h1>
<p>
  This is the home page for the
  <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
  sample application.
</p>

config/routes.rb

SecondApp::Application.routes.draw do
  get "static_pages/home"
  get "static_pages/help"
end

Gemfile

source 'https://rubygems.org'
gem 'rails', '3.2.3'
group :development do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.9.0'
  gem 'guard-rspec', '0.5.5'
end
group :assets do
  gem 'sass-rails',   '3.2.4'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
  gem 'rspec-rails', '2.9.0'
  gem 'capybara', '1.1.2'
  gem 'growl', '1.0.3'
end
group :production do
  gem 'pg', '0.12.2'
end

Any idea about what I did wrong ?

like image 810
thomaslissajoux Avatar asked May 14 '12 09:05

thomaslissajoux


2 Answers

I was having this problem as well; the tutorial seems to have missed a step.

in static_pages_spec.rb, the line:

get static_pages_index_path

...should be changed to:

get static_pages_home_path

This is because there is no index method in static_pages_controller.rb. The index is instead called home.

I reviewed your code, however, and it seems your static_pages_spec.rb file does not match the tutorial, but I guess you're copying the code from another place? I do not see static_pages_index_path anywhere, except in your console error text, which seems odd.

This is my static_pages_spec.rb in its entirety (at this stage), which passes the test:

require 'spec_helper'

describe "StaticPages" do
  describe "GET /static_pages" do
    it "works! (now write some real specs)" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get static_pages_home_path
      response.status.should be(200)
    end
  end
end

After this, the tutorial (in Listing 3.12) superseeds this step, changing static_pages_spec.rb altogether, although forgetting to explicitly recommend the change. This makes my code above irrelevant, but hopefully it explains your error.

like image 100
aljabear Avatar answered Oct 14 '22 06:10

aljabear


Have a closer look at your spec/requests/static_pages_spec.rb. Please make sure you've deleted get static_pages_index_path line.

like image 30
Anton Chikin Avatar answered Oct 14 '22 07:10

Anton Chikin