1

在阅读第 5 章第 5.3.4 节(漂亮规范)中的 Harlts 教程时。即使按照所有说明操作,我也会收到此错误。

    Static pages Contact page 
     Failure/Error: it { should have_title(full_title('Contact')) }
       expected #has_title?("Ruby on Rails Tutorial Sample App | Contact") to return true, got false
     # ./spec/requests/static_pages_spec.rb:39:in `block (3 levels) in <top (required)>'

Finished in 0.29544 seconds
11 examples, 3 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:25 # Static pages Help page 
rspec ./spec/requests/static_pages_spec.rb:32 # Static pages About page 
rspec ./spec/requests/static_pages_spec.rb:39 # Static pages Contact page

这是spec/support/utilities.rb文件的内容

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

static_pages_spec.rb文件的内容

require 'spec_helper'

describe "Static pages" do

  subject { page }

  describe "Home page" do
    before { visit root_path }

    it { should have_content('Sample App') }
    it { should have_title(full_title('home')) }
    it { should_not have_title('| Home') }
  end

  describe "Help page" do
    before { visit help_path }

    it { should have_content('Help') }
    it { should have_title(full_title('Help')) }
  end

  describe "About page" do
    before { visit about_path }

    it { should have_content('About') }
    it { should have_title(full_title('About')) }
  end

  describe "Contact page" do
    before { visit contact_path }

    it { should have_content('Contact') }
    it { should have_title(full_title('Contact')) }
  end
end
4

1 回答 1

0

主页标题文本旨在仅显示Ruby on Rails Tutorial Sample App不包含该| Home部分的内容。

所以改变

describe "Home page" do
  before { visit root_path }

  it { should have_content('Sample App') }
  it { should have_title(full_title('home')) }
  it { should_not have_title('| Home') }
end

describe "Home page" do
  before { visit root_path }

  it { should have_content('Sample App') }
  it { should have_title(full_title('')) }
  it { should_not have_title('| Home') }
end

所以删除home那里的第 5 行

于 2013-09-28T08:07:31.703 回答