1

私は、Michael Hartl の Rails チュートリアルの第 5 章の演習に参加しており、Rails/Rspec が でヘルパー メソッドfull_titleをテストする方法について頭を悩ませようとしていapp/helpers/application_helper.rbます。私のテストはすべてその中にあり、コードの肥大化を減らすためにspec/requests/static_pages_spec.rb呼び出しています。full_title

したがって、オリジナルfull_titleをテストするために、 でテストを作成し、spec/helpers/application_helpers_spec.rbを介してインクルードしspec/support/utilities.rbます。コードは通りますが、プロセス(順序から操作まで)がどうなっているのかを理解したいです。ありがとうございました。

こんな風に考えていいですか?

  1. Rspec の実行が開始されますstatic_pages_spec.rb(を含むutilities.rb)
  2. Rspec は full_title メソッドをstatic_pages_spec.rb
  3. Rspec が実行を開始しますapplication_helper_spec.rb
  4. Rspecは見describe "full_title" doますapplication_helper_spec.rb
  5. Rspecは独自のfull_titleメソッドを検索し、テストを終了しますapplication_helper_spec.rb
  6. Rspec は static_pages_spec.rb full_title` でテストを終了します, iterating through above process when

static_pages_spec.rb

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

application_helper_spec.rb

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

application_helper.rb

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
4

1 に答える 1

1

次のように考えてください。

( utilities.rb を含む) で呼び出された「 full_title」は、application_helper.rb で説明されている「full_title」メソッドを実行しています。 static_pages_spec.rb

application_helper_spec.rb、full_title を介して渡された文字列/値 (page_title) を検証しています。私が間違っていなければfull_title、テストでメソッドが呼び出されるたびにこれを行います。

于 2013-05-16T23:30:27.037 に答える