嘿,我是Rails和Rspec的新手,我只是阅读Michael Hartls Tutorial。我坚持运行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
我不知道出了什么问题。我使用了这个测试〜20次,其中包含“root_path”,但它之前从未出现过麻烦。 如果它有用,请点击这里我的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
我只是照看了我在过去20分钟内编辑的所有文件,但没有看到任何可以解释这一点的文件。
答案 0 :(得分:0)
&#34;您无法在spec类中调用路径方法。它应该在它的块中。&#34; 看看这个:Rspec undefined local variable or method root_path