2012-01-18 56 views
0

已经由迈克尔·哈特 以下的Rails教程Mac OS X上轨3.0版本10.7Rails pages_controller_spec.rb测试不应该失败,但是,错误?

$ 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 

测试看起来是这样的:

require 'spec_helper' 

describe PagesController do 
    render_views 

    describe "GET 'home'" do 
    it "should be successful" do 
     get 'home' 
     response.should be_success 
    end 

    it "should have the right title" do 
     get 'home' 
     response.should have_selector("title", 
     :content => "Ruby on Rails Tutorial Sample App | Home") 
    end 
    end 

    describe "GET 'contact'" do 
    it "should be successful" do 
     get 'contact' 
     response.should be_success 
    end 
    it "should have the right title" do 
     get 'contact' 
     response.should have_selector("title", 
     :content => "Ruby on Rails Tutorial Sample App | Contact") 
    end 
    end 

    describe "GET 'about'" do 
    it "should be successful" do 
     get 'about' 
     response.should be_success 
    end 
    it "should have the right title" do 
     get 'about' 
     response.should have_selector("title", 
     :content => "Ruby on Rails Tutorial Sample App | About") 
    end 
    end 

    describe "GET 'help'" do 
    it "should be successful" do 
     get 'help' 
     response.should be_success 
    end 
    it "should have the right title" do 
     get 'help' 
     response.should have_selector("title", 
     :content => "Ruby on Rails Tutorial Sample App | Help") 
    end 
    end 
end 

而且我有in pages_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 

而在routes.rb中我有

SampleApp::Application.routes.draw do 
    get "pages/home" 
    get "pages/contact" 
    get "pages/about" 
    get "pages/help" 
end 

当然,我也创造了在应用程序/视图/ 奇怪的是,当我运行轨道服务器并转到本地主机页面help.html.erb页:3000/pages/help我用合适的标题获得适当的页面,使其看起来好像测试应该已经通过,但事实并非如此。此外,联系人,家庭和关于测试通过,但当我刚刚添加的帮助它不是因为某些未知的原因。这真的让我感到困扰,我忽略了这个简单的解决方案,它让我疯了吗?

+0

出于某种原因,我help.html.erb文件是275个字节,而我的约,接触和家庭.html.erb页面都是2KB,尽管他们几乎是准确的每个字符的数量相同。这肯定与help.html.erb在测试时不起作用有关。此外,帮助的文档类型是TextMate文档,而其他文档只是说“文档”。奇怪..? – Laser 2012-01-18 20:38:05

+0

我认为你在正确的轨道上 - 代码中没有任何东西看起来会失败。我会尝试删除并重新创建help.html.erb文件。 – 2012-01-18 20:39:50

+0

感谢您的想法,不幸的是它没有帮助。特别奇怪的是,它给我一个错误,耙路线是矛盾的,因为在这里解释:http://stackoverflow.com/questions/8917201/my-computer-hates-me – Laser 2012-01-18 20:55:36

回答

2

下载代码并运行:

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

它运行的是得到“帮助”,所以我觉得你在自动测试运行这一点,它不重装。可能?

+0

谢谢你,退出终端,并打开和新窗口,一切运行良好。简单是不可思议的。 – Laser 2012-01-18 21:19:53

2

你的代码没问题。问题在于你之前的代码被缓存了。通过退出终端并打开新窗口,您可以清除缓存。如果您在测试环境中不关闭缓存,则可能遇到相同的问题。转到config/environments/test.rb,改变config.cache_classes = trueconfig.cache_classes = false