Michael Hartl指导第5章rspec问题

时间:2011-06-02 22:51:27

标签: ruby-on-rails-3

一旦我完成了第5章,其中标题为“填写布局”以及用户的初始创建,我运行了rspec并获得以下内容:

1) PagesController GET 'home' should have the right title
 Failure/Error: response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Home")
   expected following output to contain a <title>Ruby on Rails Tutorial Sample App | Home</title> 

2) PagesController GET 'contact' should have the right title
 Failure/Error: response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Contact")
   expected following output to contain a <title>Ruby on Rails Tutorial Sample App | Contact</title> 

3) PagesController GET 'about' should have the right title
 Failure/Error: response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | About")
   expected following output to contain a <title>Ruby on Rails Tutorial Sample App | About</title>

我一直在研究这个问题大约一天,我只是不知道我做错了什么?此外,这些页面也完美无缺

这是pagescontroller代码     需要'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
end

这里也是我的app / views / layouts / application.html.erb

<title><%= @title %></title>

这是我的layout_links_spec

require 'spec_helper'


it "should have a Home page at '/'" do
  get '/'
  response.should have_selector('title', :content => "Home")
end

it "should have a Contact page at '/contact'" do
  get '/contact'
  response.should have_selector('title', :content => "Contact")
end

it "should have have an About page at '/about" do
  get '/about'
  response.should have_selector('title', :content => "About")
end

it "should have a Help pageat '/help'" do
  get '/help'
  response.should have_selector('title', :content => "Help")
end

it "should have a signup page at '/signup'" do
  get '/signup'
  response.should have_selector('title', :content => "Sign up")
end

it "should have the right links on the layout" do
  visit root_path
  response.should have_selector('title', :content => "Home") 
end
end

2 个答案:

答案 0 :(得分:3)

确保您已包含render_views

require 'spec_helper'

describe UsersController do
  render_views

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

    it "should have the right title" do
      get :new
      response.should have_selector("title", :content => "Sign up")
    end
  end
end

答案 1 :(得分:1)

我花了一些时间来弄清楚确切的错误是什么,但请确保您按照迈克教程中的说明进行操作。

Mike非常高效和彻底,但如果你跳过一步就会出错。我看到同样的问题,首先您需要确保使用列出 5.20 的最新static_pages_spec.rb,列出 5.23 的最新路线文件并确保删除public\index.html文件。

完成这些步骤后,您的错误就会消失。