thomaslissajoux
thomaslissajoux

Reputation: 93

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 ?

Upvotes: 6

Views: 5300

Answers (8)

santana
santana

Reputation: 1

I was facing a similar problem. When you edit the static_pages_spec.rb, it looks like you typed this command (in listings 3.9)

<editor name> static_pages_spec.rb

whereas you had to type

<editor name> spec/requests/static_pages_spec.rb

This will surely solve your problem.

Upvotes: 0

AlexChaffee
AlexChaffee

Reputation: 8252

The problem is that you are trying to run a generated spec that doesn't actually work yet. The tutorial tells you to replace the body of that spec with this:

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

Note that it says "visit", not "get", and so on...

This happened because the spec generator (rails generate integration_test static_pages) assumes that there is a valid RESTful resource with lots of named routes, including _index_path, but that's simply not the case for this controller in this tutorial.

Upvotes: 0

piratebroadcast
piratebroadcast

Reputation: 291

When you add "config.include Capybara::DSL" to the bottom of the RSpec helper file, in the section right before this, you may have forgotten to save the page. That is done by pressing the Command + S keys together. Sometimes it is also a good idea to restart your Rails Server if something doesn't work the way you expect it to the first time.

Upvotes: 0

allanberry
allanberry

Reputation: 7765

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.

Upvotes: 4

Thomas
Thomas

Reputation: 11

I had the same problem and I figured it out in the following way. I am also a newbie (first Stack Overflow post... nervous), so I don't have the jargon down:

The reason I was getting this error is that I didn't create the staticpages controller described in Listing 3.4 of the tutorial (I think I deleted it messing around with the practice commands that follow Listing 3.4 teaching you how to ahem, delete things). The way to check if you don't have the staticpages controller is to go to:

sample_app/controllers/ and so I only had the application_controller.rb file there and no static_pages_controller.rb.

So you have to run the rails generate controller StaticPages home help --no-test-framework command and get that controller in there.

You can double-check your work by going to localhost:3000/static_pages/home and seeing if there is actually something there.

Then edit, per the tutorial, the home.html.erb files and check back to static_pages/home to see if it actually says "Sample App".

If static_pages/home actually says "Sample App" and the test is still a failure when you run it, then only God can help you. Or maybe someone else on Stack Overflow. Good luck.

Upvotes: 1

borgel
borgel

Reputation: 31

You have to update your app/views/static_pages/help.html.erb page to contain 'Sample App' in the same way as you have done with home.html.erb.

Upvotes: 1

Anton Chikin
Anton Chikin

Reputation: 1836

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

Upvotes: 1

Tom
Tom

Reputation: 2075

Have you deleted the public/index.html? This could be causing the problem.

Upvotes: 0

Related Questions