0

I am on Chapter 4 of Michael Hartl's RoR tutorial, and having some troubles with rspec and using the full_title helper function. In part 4.1 of the tutorial, I have the helper code as follows. It's purpose is to make it so my help, contact, home, and about pages don't need to be supplied a title on each view.

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

end

My layout page looks like

<!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>

and my static_pages_spec.rb file looks like:

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

Yet when I run rspec, I get 4 test failures. 3 of them refer to the contact, about us, and help pages and say something along the lines of

Failure/Error: :text => "#base_title} | Contact") NameError: undefined local variable or method "base_title" for #

Can anyone tell me what's going wrong here? I suspect it has something to do with the helper function ans the base_title variable not being recognized by Rspec, but according to the tutorial the test suite should be passing all green.

Update: I figured out why one of the tests was failing. But I still have a problem with those 3 views saying base_title is an undefined method.

4

1 回答 1

0

In the following test, how rspect knows what is base_title (2nd last line).

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

You have not defined it. I guess that's why your tests are failing.

于 2012-07-20T15:02:01.547 回答