2013-05-16 98 views
1

我在第5章練習Michael Hartl's Rails Tutorial,並試圖圍繞Rails/Rspec如何測試幫助程序full_titleapp/helpers/application_helper.rb。我所有的測試都在spec/requests/static_pages_spec.rb之內,我打電話給full_title來減少代碼膨脹。Rails/RSpec - 爲原始幫手方法編寫測試

因此,爲了測試原始full_title我創建了一個測試spec/helpers/application_helpers_spec.rb並通過spec/support/utilities.rb包括它。代碼正在通過,但我想了解該過程(爲了操作)以瞭解正在發生的事情。謝謝。

我可以這樣想嗎?

  1. Rspec的開始運行static_pages_spec.rb(包括utilities.rb
  2. Rspec的看到在static_pages_spec.rb
  3. Rspec的開始full_title方法來運行application_helper_spec.rb
  4. Rspec的看到describe "full_title" doapplication_helper_spec.rb
  5. Rspec的查找原始full_title方法並完成測試application_helper_spec.rb
  6. Rspec完成了static_pages_spec.rb中的測試, iterating through above process when full_title`被調用。

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 

回答

1

想一想這種方式:

'full_title'稱爲static_pages_spec.rb(包括utilities.rb)正在運行的'full_title' 在application_helper.rb描述方法。

application_helper_spec.rb正在驗證通過full_title傳遞的字符串/值(page_title)。 如果我沒有弄錯,每次在測試中調用full_title方法時都會這樣做。

+0

這是我很困惑。如果沒有測試或application_helper_spec.rb中的實際full_title方法,我在static_pages_spec.rb中的測試將失敗,並顯示'沒有full_title方法錯誤'。我明白將application_helper_spec.rb中的實際full_title方法確保測試通過,但爲什麼在application_helper_spec.rb中對原始full_title方法進行測試還能確保測試通過? – KMcA

+1

Alrighty,所以當你在application_helper_spec.rb中描述ApplicationHelper時,它會調用該模塊。下面的測試確保正在傳遞的內容是有效的/您想要的數據。 因此,在回答您的問題時,關鍵部分是 '在application_helper_spec.rb中描述ApplicationHelper'行。有了這個,測試「理解」了full_title方法,並允許您在測試中使用它。 – JohnSchaum

+0

非常感謝您的幫助。 – KMcA