2012-04-27 3 views
0

私はMichaels Hartls Ruby on Railsチュートリアルで作業しています。第3章では、指示に従ってR Specと自動テストを設定しました。これまで私はチュートリアルで指示されたすべてを行ったが、私はこのエラーメッセージを受け取り続けている。rspec。/spec/controllers/pages_controller_spec.rb:13#PagesController GET 'home'には正しいタイトルが必要です

rspec ./spec/controllers/pages_controller_spec.rb:13 # PagesController GET 'home' should have the right title 
rspec ./spec/controllers/pages_controller_spec.rb:31 # PagesController GET 'contact' should have the right title 
rspec ./spec/controllers/pages_controller_spec.rb:43 # PagesController GET 'about' should have the right title 
rspec ./spec/controllers/pages_controller_spec.rb:57 # PagesController GET 'help' should have the right title' 

マイGemfileが

source 'https://rubygems.org' 

gem 'rails', '3.2.3' 

# Bundle edge Rails instead: 
# gem 'rails', :git => 'git://github.com/rails/rails.git' 

gem 'sqlite3' 
gem 'heroku' 

group :development do 

gem 'autotest', '4.4.6' 
gem 'rspec-rails', '2.9.0' 
end 
# Gems used only for assets and not required 
# in production environments by default. 
group :assets do 
    gem 'sass-rails', '~> 3.2.3' 
    gem 'coffee-rails', '~> 3.2.1' 

group :production do 
# gems specifically for Heroku go here 
    gem 'pg' 
end 
group :test do 

gem 'rspec', '2.9.0' 
gem 'webrat', '0.7.3' 
gem "spork", '0.9.0' 

end 
    # See https://github.com/sstephenson/execjs#readme for more supported runtimes 
    gem 'therubyracer', :platform => :ruby 
    gem 'execjs' 
    gem 'uglifier', '>= 1.0.3' 
end 

gem 'jquery-rails' 

# To use ActiveModel has_secure_password 
# gem 'bcrypt-ruby', '~> 3.0.0' 

# To use Jbuilder templates for JSON 
# gem 'jbuilder' 

# Use unicorn as the app server 
# gem 'unicorn' 

# Deploy with Capistrano 
# gem 'capistrano' 

# To use debugger 
# gem 'ruby-debug19', :require => 'ruby-debug' 

表示マイapplication.html.erbは

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

マイpage_controller_spec.rbこの

require 'spec_helper' 

describe PagesController do 
render_views 
#home 

    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 => @base_title + " | Home") 
    end 

    it "should have a non-blank body" do 
    get 'home' 
    response.body.should_not =~ /<body>\s*<\/body>/ 
end 
end 
    #contact 

    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 => @base_title + " | Contact") 
    end 
    end 
#about 
    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 => @base_title + " | About") 
    end 
    end 

    #help 

    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 => @base_title + "| Help") 
    end 
    end 
    end 

pages_helper.rbが

01を持っているそれぞれ示します
module PagesHelper 


     # Return a title an 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 

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 

すべてがブラウザで良く示しています。しかし、私のテストがrspec spec /を使って実行されたときに、私はまだこれらのエラーが発生します。

誰かが助けることができますか?

+0

@base_titleが仕様で、私はそれを設定する必要があり、あなたのスペック –

+0

のいずれかの場所に設定されていないよう使用しています。私のpage_controller_spec.rbはそれを行う GET「ホーム」「右のタイトルを持っている必要があります」と表示 response.should have_selector( "title"、 :content => @base_title + "| Home") end これはどこに設定するのですか –

答えて

1

@base_titleを設定する必要があります。あなたはそれの使用に先立って、仕様の任意の場所に設定できますが、それはどこでも同じなので、あなたは

before(:each) do 
    @base_title = ... 
end 

あなたはまた、使用することができlet(しかし、その後、スペックがする必要があるすなわち、beforeブロックにそれを置くことができbase_titleではなく@base_title

+0

Ruby on Railsチュートリアルサンプルアプリケーション " @base_title ="を前に追加しました。 end'を私のpages_controller_spec.rbの先頭に追加しました。私はまだ同じエラーが表示されます。 –

+0

解決済み!彼らは問題 DEF @title = "連絡先" 終了 デフについて @title = "について" 最後までお問い合わせ 'クラスでもをPagesController

関連する問題