Rails pages_controller_spec.rb测试不应该失败,但是,错误?

时间:2012-01-18 20:29:10

标签: ruby-on-rails testing rspec routes

一直关注迈克尔哈特的Rails教程 在Mac OS X 10.7上使用rails 3.0版

$ rspec spec /

......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

我在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

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

2 个答案:

答案 0 :(得分:2)

下载您的代码并运行:

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

正在运行GET'帮助',所以我认为你在自动测试中运行它并且它没有重新加载。可能的?

答案 1 :(得分:2)

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