Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails pages_controller_spec.rb测试';不是失败,而是错误?_Ruby On Rails_Testing_Rspec_Routes - Fatal编程技术网

Ruby on rails Rails pages_controller_spec.rb测试';不是失败,而是错误?

Ruby on rails Rails pages_controller_spec.rb测试';不是失败,而是错误?,ruby-on-rails,testing,rspec,routes,Ruby On Rails,Testing,Rspec,Routes,一直在学习MichaelHart的Rails教程 mac OS X 10.7上的rails 3.0版 $rspec规格/ ......FF Failures: 1) PagesController GET 'help' should be successful Failure/Error: get 'help' ActionController::RoutingError: No route matches {:controller=>"pages

一直在学习MichaelHart的Rails教程 mac OS X 10.7上的rails 3.0版

$rspec规格/

......FF

Failures:

  1) PagesController GET 'help' should be successful
     Failure/Error: get 'help'
     ActionController::RoutingError:
       No route matches {:controller=>"pages", :action=>"help"}
     # ./spec/controllers/pages_controller_spec.rb:45:in `block (3 levels) in <top (required)>'

  2) PagesController GET 'help' should have the right title
     Failure/Error: get 'help'
     ActionController::RoutingError:
       No route matches {:controller=>"pages", :action=>"help"}
     # ./spec/controllers/pages_controller_spec.rb:49:in `block (3 levels) in <top (required)>'

Finished in 0.14686 seconds
8 examples, 2 failures

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:44 # PagesController GET 'help' should be successful
rspec ./spec/controllers/pages_controller_spec.rb:48 # PagesController GET 'help' should have the right title
我在_controller.rb中有一些页面

class PagesController < ApplicationController
  def home
    @title = "Home"
  end

  def contact
    @title = "Contact"
  end

  def about
    @title = "About"
  end

  def help
    @title = "Help"
  end

end
当然,我还在app/views/pages中创建了help.html.erb页面
奇怪的是,当我运行rails服务器并转到localhost:3000/pages/help时,我得到了正确的页面和正确的标题,使它看起来好像测试应该通过了,但它没有通过。此外,联系人、主页和关于测试都通过了,但当我刚刚添加帮助时,由于一些未知的原因,它没有通过。这真的让我烦透了,我忽略了让我发疯的简单解决方案是什么?

下载了您的代码并运行:

........
8 examples, 0 failures
Finished in 0.18184 seconds

它正在运行GET'help(获取帮助),因此我认为您正在自动测试中运行它,并且它没有重新加载。可能吗?

您的代码很好。问题是您以前的代码被缓存了。通过退出终端并打开一个新窗口,可以有效地清除缓存。如果不在测试环境中关闭缓存,可能会遇到同样的问题。转到
config/environments/test.rb
并将
config.cache_classes=true
更改为
config.cache_classes=false

出于某种原因,我的help.html.erb文件是275字节,而我的about、contact和home.html.erb页面都是2kb,尽管它们的字符数几乎完全相同。这肯定与help.html.erb在测试时不起作用有关。另外,帮助的文档类型是TextMate文档,而其他文档只说“document”。奇怪?我认为你的思路是对的——代码中没有任何东西会失败。我会尝试删除并重新创建help.html.erb文件。谢谢你的想法,不幸的是没有帮助。特别奇怪的是,它给了我一个错误,就像这里解释的那样,rake routes自相矛盾:你的另一个问题被否决并不奇怪。不要用讽刺性的标题来提问或咒骂——这是非常不受欢迎的。你为什么不把你的代码发布到github并链接到它——我会帮你看一下,让你找到正确的方向。很抱歉,这不合适,我把它推到这里了。谢谢,退出终端,打开新窗口,一切都很好。简单是神奇的。
SampleApp::Application.routes.draw do
  get "pages/home"
  get "pages/contact"
  get "pages/about"
  get "pages/help"
end
........
8 examples, 0 failures
Finished in 0.18184 seconds