Hey I'm new to Rails and Rspec and I'm just reading Michael Hartls Tutorial. I stucked at the point where I ran my static_pages_spec.rb
C:/Rails/sample_app/spec/requests/static_pages_spec.rb:48:in `block (2 levels) i
n <top (required)>': undefined local variable or method `root_path' for #<Class:
0x55795b0> (NameError)
static_pages_spec.rb:
require 'spec_helper'
describe "Static pages" do
subject { page }
shared_examples_for "all static pages" do
it { should have_selector('h1', text: heading) }
it { should have_title(full_title(page_title)) }
end
describe "Home page" do
before { visit root_path }
let(:heading) { 'Sample App' }
let(:page_title) { '' }
it_should_behave_like "all static pages"
it { should_not have_title('| Home') }
end
describe "Help page" do
before { visit help_path }
let(:heading) { 'Help' }
let(:page_title) { 'Help' }
it_should_behave_like "all static pages"
end
describe "About page" do
before { visit about_path }
let(:heading) { 'About' }
let(:page_title){ 'About' }
it_should_behave_like "all static pages"
end
describe "Contact page" do
before { visit contact_path }
let(:heading) { 'Contact' }
let(:page_title){ 'Contact' }
it_should_behave_like "all static pages"
end
describe "should have the right links on the layout" do
visit root_path
click_link "About"
expect(page).to have_title(full_title('About Us'))
click_link "Help"
expect(page).to have_title(full_title('Help'))
click_link "Contact"
expect(page).to have_title(full_title('Contact'))
click_link "Home"
click_link "Sign up now!"
expect(page).to have_title(full_title('Sign up'))
click_link "sample app"
expect(page).to have_title(full_title('Sample App'))
end
end
I don't know what's wrong. I used this test ~20 times with "root_path" in it but it never did made trouble before. If it's any useful, here my routes.rb:
SampleApp::Application.routes.draw do
root 'static_pages#home'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/signup', to: 'users#new', via: 'get'
get "users/new"
end
I just looked after all the files I edited in the last 20 minutes but saw nothing that could explain this.
"You cannot call path methods in spec class. It should be in it block." Check this out: Rspec undefined local variable or method root_path
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.