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

I follow the Rails Tutorial, but have a problem in section 3.2.1 , before Figure 3.6 . At startup

$ bundle exec rspec spec/requests/static_pages_spec.rb 

I get rejected

 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:

specifications / queries / 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 

application / 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 on what I did wrong?

+6
source share
8 answers

I also had this problem; The tutorial seems to have skipped a step.

in static_pages_spec.rb , line:

 get static_pages_index_path 

... should be changed to:

 get static_pages_home_path 

This is because static_pages_controller.rb does not have an index method. Instead, index is called home .

I looked at your code, however, it seems that your static_pages_spec.rb file does not match the tutorial, but I think you are copying the code from another place? I don't see static_pages_index_path anywhere except your console error text, which seems weird.

This is my static_pages_spec.rb completely (at this point) that 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 that, the tutorial (in listing 3.12 ) will replace this step by changing static_pages_spec.rb in general, although I forgot to explicitly recommend a change. This makes my code above inappropriate, but hopefully it explains your error.

+3
source

I had the same problem, and I understood it as follows. I'm also a beginner (first post ... nervous), so I don't have jargon:

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

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

So, you should run rails generate controller StaticPages home help --no-test-framework and get this controller there.

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

Then edit the home.html.erb files in the tutorial and go back to static_pages/home to see if it really says “Sample application”.

If static_pages/home actually says “Sample application” and the test is still a failure when it runs, then only God can help you. Or maybe someone else at Stack Overflow. Good luck.

+1
source

Check out your spec/requests/static_pages_spec.rb . Make sure you remove the get static_pages_index_path line.

+1
source

You need to refresh the application page / views / static _pages / help.html.erb to contain the "Application Example" just like you did with home.html.erb.

+1
source

Did you delete public / index.html? This can cause problems.

0
source

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

 <editor name> static_pages_spec.rb 

whereas you should have entered

 <editor name> spec/requests/static_pages_spec.rb 

This will definitely solve your problem.

0
source

When you add "config.include Capybara :: DSL" to the end of the RSpec helper file, in the section right before that, you may have forgotten to save the page. This is done by simultaneously pressing Command + S. Sometimes it is also recommended to restart the Rails server if something does not work the way you expect it the first time.

0
source

The problem is that you are trying to run a generated specification that is not working yet. In the tutorial, you are asked to replace the body of this specification with the following:

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

Please note that he says “visit” rather than “receive” etc ....

This happened because the spec generator ( rails generate integration_test static_pages ) assumes that there is a valid RESTful resource with a lot of named routes, including _index_path , but this just doesn’t apply to this controller in this tutorial.

0
source

Source: https://habr.com/ru/post/915650/


All Articles