【问题标题】:Rails/RSpec - writing tests for original helper methodsRails/RSpec - 为原始辅助方法编写测试
【发布时间】:2013-05-11 16:50:13
【问题描述】:

我正在参加Michael Hartl's Rails Tutorial 的第 5 章练习,并试图了解 Rails/Rspec 如何在 app/helpers/application_helper.rb 中测试辅助方法 full_title。我所有的测试都在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 在static_pages_spec.rb 中看到full_title 方法
  3. Rspec 开始运行application_helper_spec.rb
  4. Rspec 在application_helper_spec.rb 中看到describe "full_title" do
  5. Rspec 查找原始full_title 方法并完成对application_helper_spec.rb 的测试
  6. Rspec 在调用 static_pages_spec.rb, iterating through above process whenfull_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

【问题讨论】:

    标签: ruby-on-rails rspec helpers


    【解决方案1】:

    这样想:

    static_pages_spec.rb(包括 utility.rb)中调用的 'full_title' 正在运行 application_helper.rb 中描述的 'full_title' 方法。

    application_helper_spec.rb 正在验证通过 full_title 传递的字符串/值 (page_title)。 如果我没记错的话,每次在您的测试中调用 full_title 方法时都会这样做。

    【讨论】:

    • 这就是我感到困惑的地方。如果没有测试或 application_helper_spec.rb 中的实际 full_title 方法,我在 static_pages_spec.rb 中的测试将失败,并出现“无 full_title 方法错误”。我知道在 application_helper_spec.rb 中放置实际的 full_title 方法可以确保测试通过,但为什么在 application_helper_spec.rb 中对原始 full_title 方法进行测试也可以确保测试通过?
    • 好吧,所以当您在 application_helper_spec.rb 中描述 ApplicationHelper 时,它正在调用该模块。下面的测试确保通过的内容是有效的/您想要的数据。因此,回答您的问题的关键部分是 application_helper_spec.rb 中的“描述 ApplicationHelper”行。有了这个,测试“理解”了 full_title 方法并允许你在测试中使用它。
    • 非常感谢您的帮助。
    • 没问题,我自己刚看完这本书。 Rails 中发生了很多神奇的事情。
    • 太棒了。在接下来的章节中我应该注意什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2011-08-03
    • 2012-08-19
    • 2013-03-06
    • 2014-11-19
    相关资源
    最近更新 更多