RoR,第5章,期待#has_title?(" Ruby on Rails教程示例应用程序|关于我们")返回true,得到错误

时间:2015-01-26 13:40:59

标签: ruby-on-rails ruby ruby-on-rails-4 rspec

我正在研究Michael Hartl的Ruby on Rails教程,我正在进行第5章练习。有人可以解释为什么这个测试失败了吗?

这是spec / requests / static_pages_spec.rb

require 'spec_helper'

describe "Static pages" do

#  let(:base_title) { "Ruby on Rails Tutorial Sample App" }

  subject { page }

  shared_examples_for "all static pages" do
    it { should have_selector('h1', text: heading) }
    it { should have_title(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_title('| Home') }
  end

  describe "Help page" do
    before { visit help_path }

    let(:heading) { 'Help' }
    let(:page_title) { '' }

    it_should_behave_like  "all static pages"
    it { should_not have_title('| Help') }
  end

  describe "About page" do
    before { visit about_path }

    let(:heading) { 'About' }
    let(:page_title) { '' }

    it_should_behave_like  "all static pages"
    it { should_not have_title('| About Us') }
  end

  describe "Contact page" do
    before { visit contact_path }

    let(:heading) { 'Contact' }
    let(:page_title) { '' }

    it_should_behave_like  "all static pages"
    it { should_not have_title('| Contact') }
  end

  it "should have the right links on the layout" do
    visit root_path
    click_link "About"
    expect(page).to have_title(full_title('About Us'))
    click_link "Help"
    expect(page).to have_title(full_title('Help'))
    click_link "Contact"
    expect(page).to have_title(full_title('Contact'))
    click_link "Home"
    click_link "Sign up now!"
    expect(page).to have_title(full_title('Sign up'))
    click_link "sample app"
    expect(page).to have_title(full_title('Sample App'))
  end

end

这是app / views / static_pages / about.html.erb

<h1>About Us</h1>
    <p>
      The <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
      is a project to make a book and screencasts to teach web development
      with <a href="http://rubyonrails.org/">Ruby on Rails</a>. This
      is the sample application for the tutorial.
    </p>

我的测试rspec spec /

Failures:

  1) Static pages should have the right links on the layout
     Failure/Error: expect(page).to have_title(full_title('About us'))
       expected #has_title?("Ruby on Rails Tutorial Sample App | About us") to return true, got false
     # ./spec/requests/static_pages_spec.rb:57:in `block (2 levels) in <top (required)>'

Finished in 1.11 seconds
13 examples, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:54 # Static pages should have the right links on the layout

1 个答案:

答案 0 :(得分:0)

  

有人可以解释为什么这个测试失败了吗?

您的问题似乎是页面的完整title为:

"Ruby on Rails Tutorial Sample App | About us" "About us"

测试套件告诉你的是什么。我想你可能期望base_title是空的&#34;。 (最顶层的let(:base_title))?