title: Rspec undefined local variable or method root_path
body: <p>I'm starting to use Rspec, but when I run <code>bundle exec rspec</code> I get an error </p>
  
  <pre><code> /spec/requests/pages_spec.rb:20:in `block (2 levels) in &lt;top (required)&gt;': undefined local
   variable or method `root_path' for #&lt;Class:0x00000102e46850&gt; (NameError)
  </code></pre>
  
  <p>I do not have Spork or Guard running so the question below shouldn't apply</p>
  
  <p><a href="https://stackoverflow.com/questions/14871918/undefined-local-variable-or-method-root-path-rspec-spork-guard">undefined local variable or method `root_path&#39; (Rspec Spork Guard)</a></p>
  
  <p>I have added <code>config.include Rails.application.routes.url_helpers</code> in my <code>spec_helper.rb</code> file, but that did not help.
  <a href="https://stackoverflow.com/questions/12240868/undefined-local-variable-or-method-root-path-hartls-tutorial-chapter-5-3-2">undefined local variable or method `root_path&#39; Hartl&#39;s Tutorial Chapter 5.3.2</a></p>
  
  <p>Here's <code>pages_spec.rb</code></p>
  
  <pre><code>require 'spec_helper'                                                                                                                                                       
  
  describe "Pages" do                                                                                                                                                         
    describe "navigation" do                                                                                                                                                  
  
      def self.it_should_be_on(path_name, value=nil)                                                                                                                          
        before { visit path_name }                                                                                                                                            
  
        it "should be on #{path_name}" do                                                                                                                                     
          page.should have_link('Home')                                                                                                                                       
          page.should have_link('Inventory')                                                                                                                                  
          page.should have_link('FAQ')                                                                                                                                        
          page.should have_link('About Us')                                                                                                                                   
          page.should have_link('Location')                                                                                                                                   
          page.should have_link('Contact Us')                                                                                                                                 
          # page.should have_link('Login')                                                                                                                                    
        end                                                                                                                                                                   
      end                                                                                                                                                                     
  
      it_should_be_on root_path                                                                                                                                               
      it_should_be_on faq_path                                                                                                                                                
      it_should_be_on about_path                                                                                                                                              
      it_should_be_on location_path                                                                                                                                           
      it_should_be_on contact_path                                                                                                                                            
      # it_should_be_on login_path                                                                                                                                            
    end                                                                                                                                                                       
  end       
  </code></pre>
  
  <p><code>spec_helper.rb</code></p>
  
  <pre><code># 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 }                                                                                                         
  
  # Checks for pending migrations before tests are run.                                                                                                                       
  # If you are not using ActiveRecord, you can remove this line.                                                                                                              
  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)                                                                                                 
  
  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"                                                                                                                                                   
    config.include Capybara::DSL                                                                                                                                              
    config.include Rails.application.routes.url_helpers                                                                                                                       
  end                                              
  </code></pre>
  
  <p><strong>Update</strong>
  After reading about shared_examples, I tried this out successfully. Is there a better way to write this test? I ended up separating out the pages into individual pages like Home page etc.</p>
  
  <pre><code>require 'spec_helper'                                                               
  
  describe "Pages" do                                                                 
  
    subject { page }                                                                  
  
    shared_examples "navigation" do |path_name|                                       
      before { visit send( path_name) }                                               
  
      describe "navigation links should be on #{path_name}" do                        
  
        it { should have_link('Home') }                                               
        it { should have_link('Inventory') }                                          
        it { should have_link('FAQ') }                                                
        it { should have_link('About Us') }                                           
        it { should have_link('Location') }                                           
        it { should have_link('Contact Us') }                                         
        # it { should have_link('Login') }                                            
      end                                                                             
    end                                                                               
  
    describe "Home Page" do                                                           
      include_examples "navigation", :root_path                                       
    end                                                                               
  end            
  </code></pre>