简体   繁体   English

Rails URL助手NameError

[英]Rails URL Helpers NameError

Hey I'm new to Rails and Rspec and I'm just reading Michael Hartls Tutorial. 嘿,我是Rails和Rspec的新手,我只是在阅读Michael Hartls教程。 I stucked at the point where I ran my static_pages_spec.rb 我停留在运行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: 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. 我在此测试中使用了约20次“ root_path”,但之前从未出现过麻烦。 If it's any useful, here my routes.rb: 如果有什么用,这里是我的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. 我只是照顾了过去20分钟内编辑的所有文件,但看不到任何可以解释的文件。

"You cannot call path methods in spec class. It should be in it block." “您不能在spec类中调用路径方法。它应该在它的块中。” Check this out: Rspec undefined local variable or method root_path 检查一下: Rspec未定义的局部变量或方法root_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM