Topic: undefined local variable or method [SOLVED]

i am new to TDD and have some tests that are not passing but the site is working properly

require 'spec_helper'

describe "Static pages" do

  subject { page }

  describe "Home page" do
    before { visit root_path }

    it { should have_selector('h1',    text: 'Sample App') }
    it { should have_selector('title', text: full_title('')) }
    it { should_not have_selector 'title', text: '| Home' }
  end

  describe "Help page" do
    before { visit help_path }

    it { should have_selector('h1',    text: 'Help') }
    it { should have_selector('title', text: full_title('Help')) }
  end

  describe "About page" do
    before { visit about_path }

    it { should have_selector('h1',    text: 'About') }
    it { should have_selector('title', text: full_title('About Us')) }
  end

  describe "Contact page" do
    before { visit contact_path }

    it { should have_selector('h1',    text: 'Contact') }
    it { should have_selector('title', text: full_title('Contact')) }
  end
end

OUTPUT FROM bundle exec rspec spec/requests/static_pages_spec.rb

FFFFFFFFF

Failures:

  1) Static pages Home page 
     Failure/Error: before { visit root_path }
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f825efe7710>
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) Static pages Home page 
     Failure/Error: before { visit root_path }
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f825ef2c0f0>
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  3) Static pages Home page 
     Failure/Error: before { visit root_path }
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f825eff31c8>
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  4) Static pages Help page 
     Failure/Error: before { visit help_path }
     NameError:
       undefined local variable or method `help_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007f825ec22df0>
     # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

  5) Static pages Help page 
     Failure/Error: before { visit help_path }
     NameError:
       undefined local variable or method `help_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007f825e8233d0>
     # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

  6) Static pages About page 
     Failure/Error: before { visit about_path }
     NameError:
       undefined local variable or method `about_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x007f825ee73e60>
     # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'

  7) Static pages About page 
     Failure/Error: before { visit about_path }
     NameError:
       undefined local variable or method `about_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x007f825ca80bc0>
     # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'

  8) Static pages Contact page 
     Failure/Error: before { visit contact_path }
     NameError:
       undefined local variable or method `contact_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007f825d853018>
     # ./spec/requests/static_pages_spec.rb:30:in `block (3 levels) in <top (required)>'

  9) Static pages Contact page 
     Failure/Error: before { visit contact_path }
     NameError:
       undefined local variable or method `contact_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007f825e864a60>
     # ./spec/requests/static_pages_spec.rb:30:in `block (3 levels) in <top (required)>'

Finished in 0.05946 seconds
9 examples, 9 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:10 # Static pages Home page 
rspec ./spec/requests/static_pages_spec.rb:11 # Static pages Home page 
rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page 
rspec ./spec/requests/static_pages_spec.rb:18 # Static pages Help page 
rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Help page 
rspec ./spec/requests/static_pages_spec.rb:25 # Static pages About page 
rspec ./spec/requests/static_pages_spec.rb:26 # Static pages About page 
rspec ./spec/requests/static_pages_spec.rb:32 # Static pages Contact page 
rspec ./spec/requests/static_pages_spec.rb:33 # Static pages Contact pag


rake routes

root  /                  static_pages#home
   help  /help(.:format)    static_pages#help
  about  /about(.:format)   static_pages#about
contact  /contact(.:format) static_pages#contact

config/routes.rb

Tweeter::Application.routes.draw do
  root to: 'static_pages#home'
#  match '/', to: 'static_pages#home'
  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
end

Thanks for all the help

Last edited by moiseszaragoza (2012-05-22 19:08:38)

Re: undefined local variable or method [SOLVED]

Try adding this:

describe "Static pages" do
    include Rails.application.routes.url_helpers
    ...