2012-01-08 13 views
0

Rspecを実行すると、同じタイプのエラーが3つ発生します。ここでRspecテストに失敗しますか?

マイapplication_controller.rbはこれです:

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

マイpages_controller_spec.rbはこれです:

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 

マイアプリケーション:マイエラー読み出し

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 

はこれです。 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のものだったかもしれません) –

+0

pages_controller.rbファイルを投稿してください。 – nmott

+0

@nmott。 。 。私は私の質問でpages_controller_spec.rb全体を投稿しました。 – zkidd

答えて

2

コントローラーからタイトルを設定するのではなく、ヘルパーメソッドを使用しているので、application.html.erbを@titleではなくtitleと変更してください。

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

はい。それは実際に働いた。ありがとう! – zkidd

関連する問題