2012-01-08 93 views
0

我運行Rspec時遇到了三種相同類型的錯誤。爲什麼Rspec測試在這裏失敗?

我application_controller.rb是這樣的:

<!DOCTYPE> 
<html> 
     <head> 
      <title><%= @title %></title> 
      <%= csrf_meta_tag %> 
      </head> 
     <body> 
      <%= yield %> 
     </body> 
</html> 

我pages_controller_spec.rb是這樣的:

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 | Home") 
    end 

    it "should have a non-blank body" do 
     get 'home' 
     response.body.should_not =~ /<body>\s*<\/body>/ 
    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 | 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 | About") 
    end 
    end 
end 

我的錯誤讀出的是這樣的:

7 examples, 3 failures 

Failed examples: 

    rspec ./spec/controllers/pages_controller_spec.rb:12 # PagesController GET 'home' should have the right title 
rspec ./spec/controllers/pages_controller_spec.rb:30 # PagesController GET 'contact' should have the right title 
rspec ./spec/controllers/pages_controller_spec.rb:43 # PagesController GET 'about' should have the right title 

我的應用程序。 html.erb

<!DOCTYPE> 
<html> 
     <head> 
      <title><%= @title %></title> 
      <%= csrf_meta_tag %> 
      </head> 
     <body> 
      <%= yield %> 
     </body> 
</html> 

這是我的幫助文件:

module ApplicationHelper 

    # Return a title on a per-page basis. 
    def title 
    base_title = "Ruby on Rails Tutorial Sample App" 
    if @title.nil? 
     base_title 
    else 
     "#{base_title} | #{@title}" 
    end 
    end 
end 

這裏是我的網頁控制器文件:

class PagesController < ApplicationController 

     def home 
     @title = "Home" 
     end 

     def contact 
     @title = "Contact" 
     end 

     def about 
     @title = "About" 
     end 
    end 

我不清楚,爲什麼這是失敗的。

+0

標題是什麼? (IIRC我有同樣的問題,由於檢索標題標籤內容的問題,我不記得我是如何解決這個問題的,可能是水豚v webrat的事情) – 2012-01-08 02:06:43

+0

請發佈您的pages_controller.rb文件。 – nmott 2012-01-08 12:16:47

+0

@nmott。 。 。我在我的問題中發佈了整個pages_controller_spec.rb。 – zkidd 2012-01-10 01:38:55

回答

2

將您的application.html.erb更改爲title而不是@title,因爲您使用的是輔助方法,而不是從控制器設置標題。

<!DOCTYPE> 
<html> 
    <head> 
     <title><%= title %></title> 
     <%= csrf_meta_tag %> 
     </head> 
    <body> 
     <%= yield %> 
    </body> 
</html> 
+0

是的。這實際上工作。謝謝! – zkidd 2012-01-10 03:42:42