undefined局部变量或方法`root_path’Hartl的教程第5.3.2节

所以关于Stackoverflow的第一个问题….. =]

我在Michael Hartl的RoR教程第5.3.2章中的测试都失败了,当教程说它应该通过时(教程说’关于’,’联系’,’帮助’应该通过……但是我的全部失败了) 。 现在一切都很好,但我改变了

get 'static_pages/help' 

 match '/help', to: 'static_pages#help' 

这样我的

配置/ routes.rb中

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

然后一切都变得很糟糕。 我的所有测试都失败了一个未定义的局部变量或方法’root_path’或’about_path’等(见下面的终端输出)。 但这是我的相关文件……万一有人好奇,所有文件都是Hartl所拥有的。 我基本上复制并粘贴了每个文件的内容。

有人能帮我吗?????? 谢谢!

投机/ static_pages_spec.rb

 require 'spec_helper' describe "Static pages" do describe "Home page" do it "should have the h1 'Sample App'" do visit root_path page.should have_selector('h1', text: 'Sample App') end it "should have the base title" do visit root_path page.should have_selector('title', text: "Ruby on Rails Tutorial Sample App") end it "should not have a custom page title" do visit root_path page.should_not have_selector('title', text: '| Home') end end describe "Help page" do it "should have the h1 'Help'" do visit help_path page.should have_selector('h1', text: 'Help') end it "should have the title 'Help'" do visit help_path page.should have_selector('title', text: "Ruby on Rails Tutorial Sample App | Help") end end describe "About page" do it "should have the h1 'About'" do visit about_path page.should have_selector('h1', text: 'About Us') end it "should have the title 'About Us'" do visit about_path page.should have_selector('title', text: "Ruby on Rails Tutorial Sample App | About Us") end end describe "Contact page" do it "should have the h1 'Contact'" do visit contact_path page.should have_selector('h1', text: 'Contact') end it "should have the title 'Contact'" do visit contact_path page.should have_selector('title', text: "Ruby on Rails Tutorial Sample App | Contact") end end end 

输入’rspec spec /’的终端输出

 FFFFFFFFF Failures: 1) Static pages Contact page should have the h1 'Contact' Failure/Error: visit contact_path NameError: undefined local variable or method `contact_path' for # # ./spec/requests/static_pages_spec.rb:55:in `block (3 levels) in ' 2) Static pages Contact page should have the title 'Contact' Failure/Error: visit contact_path NameError: undefined local variable or method `contact_path' for # # ./spec/requests/static_pages_spec.rb:60:in `block (3 levels) in ' 3) Static pages Help page should have the h1 'Help' Failure/Error: visit help_path NameError: undefined local variable or method `help_path' for # # ./spec/requests/static_pages_spec.rb:27:in `block (3 levels) in ' 4) Static pages Help page should have the title 'Help' Failure/Error: visit help_path NameError: undefined local variable or method `help_path' for # # ./spec/requests/static_pages_spec.rb:32:in `block (3 levels) in ' 5) Static pages About page should have the h1 'About' Failure/Error: visit about_path NameError: undefined local variable or method `about_path' for # # ./spec/requests/static_pages_spec.rb:41:in `block (3 levels) in ' 6) Static pages About page should have the title 'About Us' Failure/Error: visit about_path NameError: undefined local variable or method `about_path' for # # ./spec/requests/static_pages_spec.rb:46:in `block (3 levels) in ' 7) Static pages Home page should not have a custom page title Failure/Error: visit root_path NameError: undefined local variable or method `root_path' for # # ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in ' 8) Static pages Home page should have the base title Failure/Error: visit root_path NameError: undefined local variable or method `root_path' for # # ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in ' 9) Static pages Home page should have the h1 'Sample App' Failure/Error: visit root_path NameError: undefined local variable or method `root_path' for # # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in ' Finished in 0.30216 seconds 9 examples, 9 failures Failed examples: rspec ./spec/requests/static_pages_spec.rb:54 # Static pages Contact page should have the h1 'Contact' rspec ./spec/requests/static_pages_spec.rb:59 # Static pages Contact page should have the title 'Contact' rspec ./spec/requests/static_pages_spec.rb:26 # Static pages Help page should have the h1 'Help' rspec ./spec/requests/static_pages_spec.rb:31 # Static pages Help page should have the title 'Help' rspec ./spec/requests/static_pages_spec.rb:40 # Static pages About page should have the h1 'About' rspec ./spec/requests/static_pages_spec.rb:45 # Static pages About page should have the title 'About Us' rspec ./spec/requests/static_pages_spec.rb:18 # Static pages Home page should not have a custom page title rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page should have the base title rspec ./spec/requests/static_pages_spec.rb:7 # Static pages Home page should have the h1 'Sample App' 

Mischa的答案有望解决大部分问题,但您的测试可能仍会在root_path问题上失败。

您是否删除了index.html: git rm public/index.html

并且不要忘记使用以下命令提交更改: git commit -am "Message"

我会把这个作为答案,因为解决方案(来自@mischa)隐藏在@ marflar的答案下的评论中:尝试重新启动Spork来解决这个问题 。

我在本章中遇到同样的问题,只需将config / routes.rb root更改为:’ static_pages #home’ root:to =>’ static_pages #home ,它就开始工作了。 你也可以用rake路线检查所有路线

在你的Gemfile中,将rspec-rails行更改为:gem’rspec-rails’,’2.12.0′

此版本的rspec支持Hartl在本书中使用的方法。

  root :to => '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' 

适合我

测试结果:9个例子0个失败

url现在似乎已经改变了:

 http://localhost:3000/about 

我使用相同的教程,遇到了完全相同的问题! 你可能以为这是固定这个的spork,但那不是它。 它起作用的原因是因为你继续并在下一节中升级了Rspec。 它只适用于升级的rspec – 旧的rspec是你的测试没有通过的原因。 该网站是错误的,它告诉用户测试将在更改Rspec之前通过

我有一个类似的问题。 我的修复是在spec_helper.rb中包含以下行

 config.infer_spec_type_from_file_location! 

BTWgem是:

 gem 'rails', '4.1.5' gem 'rspec-rails', '~> 3.0' 

我还删除了所有Guard和Spork的东西。