0

Michael Hartl の RoR チュートリアルの第 4 章を読んでいて、rspec と full_title ヘルパー関数の使用に問題があります。チュートリアルのパート 4.1 には、次のようなヘルパー コードがあります。目的は、私のヘルプ、連絡先、ホーム、およびについてのページに、各ビューでタイトルを付ける必要がないようにすることです。

module ApplicationHelper

# Returns the full title on a per-page basis.
def full_title(page_title)
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
        base_title
    else
        "#{base_title} | #{page_title}"
    end
end

終わり

私のレイアウトページは次のようになります

<!DOCTYPE html>
<html>
  <head>
    <title><title><%= full_title(yield(:title)) %></title></title>
    <%= stylesheet_link_tag    "application", :media => "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

私の static_pages_spec.rb ファイルは次のようになります。

require 'spec_helper'

describe "Static Pages" do

  include ApplicationHelper


    describe "Home page" do

        it "should have the h1 'Sample App'" do
            visit '/static_pages/home'
            page.should have_selector('h1', :text => 'Sample App')
      end
      it "should have the base title 'Home'" do
        visit '/static_pages/home'
        page.should have_selector('title',
                              :text => "Ruby on Rails Tutorial App")

      end
      it "should not have a custom page title" do
        visit '/static_pages/home'
        page.should_not have_selector('title', :text => '| Home')
      end
  end

  describe "Help page" do

    it "should have the h1 'Help'" do
        visit '/static_pages/help'
        page.should have_selector('h1', :text => 'Help')
    end
    it "should have the right title 'Help'" do
        visit '/static_pages/help'
        page.should have_selector('title',
                              :text => "#{base_title} | Help")
    end
  end

  describe "About us" do

    it "should have the h1 'About Us'" do
      visit '/static_pages/about'
      page.should have_selector("h1", :text => "About Us")
    end
    it "should have the right title 'About Us'" do
        visit '/static_pages/about'
        page.should have_selector('title',
                              :text => "#{base_title} | About Us")
    end
  end

  describe "Contact" do

    it "should have the h1 'Contact'" do
      visit '/static_pages/contact'
      page.should have_selector('h1', :text => "Contact")
    end
    it "should have the right title 'Contact'" do
        visit '/static_pages/contact'
        page.should have_selector('title',
                              :text => "#{base_title} | Contact")
    end
  end
end

しかし、rspec を実行すると、4 つのテスト エラーが発生します。そのうちの 3 人は、連絡先、私たちについて、ヘルプ ページに言及し、次のようなことを言います。

失敗/エラー: :text => "#base_title} | Contact") NameError: 未定義のローカル変数またはメソッド "base_title" for #

ここで何が問題なのか誰か教えてもらえますか? ヘルパー関数とbase_title変数がRspecによって認識されていないことに関係があると思われますが、チュートリアルによると、テストスイートはすべて緑色に合格するはずです。

更新:テストの 1 つが失敗した理由がわかりました。しかし、これらの 3 つのビューには、base_title が未定義のメソッドであるという問題がまだあります。

4

1 に答える 1