Rails/RSpec-编写针对原始助手方法的测试 [英] Rails/RSpec - writing tests for original helper methods

查看:73
本文介绍了Rails/RSpec-编写针对原始助手方法的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Michael Hartl的Rails的第5章练习教程,并试图围绕Rails/Rspec如何测试app/helpers/application_helper.rb中的帮助器方法full_title的问题.我所有的测试都在spec/requests/static_pages_spec.rb中,并且在其中,我正在调用full_title来减少代码膨胀.

I'm on the chapter 5 exercises for Michael Hartl's Rails Tutorial and am trying to wrap my head around how Rails/Rspec is testing a helper method full_title in app/helpers/application_helper.rb. All of my tests are in spec/requests/static_pages_spec.rb and within them, I'm calling full_title to cut down on code bloat.

因此,为了测试原始的full_title,我在spec/helpers/application_helpers_spec.rb中创建了一个测试,并通过spec/support/utilities.rb包括了它.代码正在传递,但是我想了解发生的事情的过程(操作的顺序).谢谢你.

So, in order to test the original full_title I create a test in spec/helpers/application_helpers_spec.rb and include it via spec/support/utilities.rb. The code is passing, but I want to understand the process (order to operations) to what is going on. Thank you.

我能这样想吗?

  1. Rspec开始运行static_pages_spec.rb(包括utilities.rb)
  2. Rspec在static_pages_spec.rb
  3. 中看到了full_title方法
  4. Rspec开始运行application_helper_spec.rb
  5. Rspec在application_helper_spec.rb中看到describe "full_title" do
  6. Rspec查找原始的full_title方法并完成对application_helper_spec.rb的测试
  7. Rspec在static_pages_spec中完成测试.rb, iterating through above process when full_title`被调用.
  1. Rspec begins to run static_pages_spec.rb (including utilities.rb)
  2. Rspec sees the full_title method in static_pages_spec.rb
  3. Rspec begins to run application_helper_spec.rb
  4. Rspec sees describe "full_title" do in application_helper_spec.rb
  5. Rspec looks up original full_title method and finishes test for application_helper_spec.rb
  6. Rspec finishes tests in static_pages_spec.rb, iterating through above process whenfull_title` is called.

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

推荐答案

以这种方式思考:

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

The 'full_title' called in static_pages_spec.rb (including utilities.rb) is running the 'full_title' method described in application_helper.rb.

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

The application_helper_spec.rb is validating the string/value (page_title) passed through full_title. If I'm not mistaken, it does this each time the full_title method is called in your tests.

这篇关于Rails/RSpec-编写针对原始助手方法的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆