0

Michaels Hartls RubyonRailsチュートリアルを使用しています。第3章では、指示に従ってR仕様と自動テストを設定しました。これまでのところ、チュートリアルで指示されたすべてのことを実行しましたが、このエラーメッセージが表示され続けます。

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には

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 / .を使用してテストを実行すると、これらのエラーが発生します。

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

4

1 に答える 1

1

を設定する必要があります@base_title。使用する前に仕様のどこにでも設定できますが、beforeブロック内に配置できる場所はどこでも同じであるため、つまり

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

使用することもできますlet(ただし、仕様base_titleではなく使用する必要があります@base_title

于 2012-04-27T20:04:07.867 に答える