title: Rails/RSpec - writing tests for original helper methods
body: <p>I'm on the chapter 5 exercises for <a href="http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec-layout_exercises" rel="nofollow">Michael Hartl's Rails Tutorial</a> and am trying to wrap my head around how Rails/Rspec is testing a helper method <code>full_title</code> in <code>app/helpers/application_helper.rb</code>. All of my tests are in <code>spec/requests/static_pages_spec.rb</code> and within them, I'm calling <code>full_title</code> to cut down on code bloat.</p>
  
  <p>So, in order to test the original <code>full_title</code> I create a test in <code>spec/helpers/application_helpers_spec.rb</code> and include it via <code>spec/support/utilities.rb</code>. The code is passing, but I want to understand the process (order to operations) to what is going on. Thank you. </p>
  
  <p>Can I think of it like this?</p>
  
  <ol>
  <li>Rspec begins to run <code>static_pages_spec.rb</code> (including <code>utilities.rb</code>)</li>
  <li>Rspec sees the full_title method in <code>static_pages_spec.rb</code> </li>
  <li>Rspec begins to run <code>application_helper_spec.rb</code> </li>
  <li>Rspec sees <code>describe "full_title" do</code> in <code>application_helper_spec.rb</code> </li>
  <li>Rspec looks up original <code>full_title</code> method and finishes test for <code>application_helper_spec.rb</code> </li>
  <li>Rspec finishes tests in static_pages_spec.rb<code>, iterating through above process when</code>full_title` is called.</li>
  </ol>
  
  <p><strong>static_pages_spec.rb</strong></p>
  
  <pre><code>require 'spec_helper'
  
  describe "Static pages" do
  
    subject { page }
  
    shared_examples_for "all static pages" do
      it { should have_selector('h1',    text: heading) }
      it { should have_selector('title', text: full_title(page_title)) }
    end
  
    describe "Home page" do
      before { visit root_path }
      let(:heading)    { 'Sample App' }
      let(:page_title) { '' }
  
      it_should_behave_like "all static pages"
      it { should_not have_selector 'title', text: '| Home' }
    end
  
    describe "Help page" do
      before { visit help_path }
      let(:heading) { 'Help' }
      let(:page_title) { 'Help' }
  
      it_should_behave_like "all static pages"
    end
  
    describe "About page" do
      before { visit about_path }
      let(:heading) { 'About' }
      let(:page_title) { 'About Us' }
  
      it_should_behave_like "all static pages"
    end
  
    describe "Contact page" do
      before { visit contact_path }
      let(:heading) { 'Contact' }
      let(:page_title) { 'Contact' }
  
      it_should_behave_like "all static pages"   
    end
  
    it "should have the right links on the layout" do
      visit root_path
      click_link "About"
      page.should have_selector 'title', text: full_title('About Us')
      click_link "Help"
      page.should have_selector 'title', text: full_title('Help')
      click_link "Contact"
      page.should have_selector 'title', text: full_title('Contact')
      click_link "Home"
      page.should have_selector 'title', text: full_title('')
      click_link "Sign up now!"
      page.should have_selector 'title', text: full_title('Sign up')
      click_link "sample app"
      page.should_not have_selector 'title', text: full_title('| Home')
    end
  end
  </code></pre>
  
  <p><strong>application_helper_spec.rb</strong></p>
  
  <pre><code>require 'spec_helper'
  
  describe ApplicationHelper do
  
    describe "full_title" do
      it "should include the page title" do
        full_title("foo").should =~ /foo/
      end
  
      it "should include the base title" do
        full_title("foo").should =~ /^Ruby on Rails Tutorial Sample App/
      end
  
      it "should not include a bar for the home page" do
        full_title("").should_not =~ /\|/
      end
    end
  end
  </code></pre>
  
  <p><strong>application_helper.rb</strong></p>
  
  <pre><code>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
  </code></pre>