1

我正在阅读 Michael Hartl 编写的Ruby on Rails 教程,但我不断收到一个我无法弄清楚的失败测试。如果您想要github 上的代码,这里是代码的链接

这是我遇到的失败:

1) Static pages should have the right links on the layout
[31mFailure/Error:[0m [31mpage.should have_selector 'title', text: full_
title('About Us')[0m
[31mexpected css "title" with text "Ruby on Rails Tutorial Sample App |
About Us" to return something[0m    
[36m     # ./spec/requests/static_pages_spec.rb:52:in `block (2 levels) in <top
(required)>'[0m
Finished in 0.3432 seconds
[31m15 examples, 1 failure[0m

Failed examples:
[31mrspec ./spec/requests/static_pages_spec.rb:49[0m [36m# Static pages shoul
d have the right links on the layout[0m

规范/请求/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) { '' }

it_should_behave_like "all static pages"  
end

describe "About page" do
before { visit about_path }

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

it_should_behave_like "all static pages"
end

describe "Contact page" do
before { visit contact_path }

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

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"
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

app/helpers/application_helper.rb

module ApplicationHelper
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    

app\views\static_pages\home.html.erb

<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>

<h2>This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.</h2>

<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>

应用程序/视图/布局/_footer.html.erb

<footer class= "footer">
<small>
    <a href="http://railstutorial.org/">Rails Tutorial</a> by Michael Hartl
</small>
<nav>
    <ul>
        <li><%= link_to "About",    about_path %></li>
        <li><%= link_to "Contact",  contact_path %></li>
        <li><a href="http://news.railstutorial.org/">News</a></li>
    </ul>
</nav>
</footer>

应用程序/视图/布局/_header.html.erb

<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
  <%= link_to "sample app", root_path, id: "logo" %>
  <nav>
    <ul class="nav pull-right">
      <li><%= link_to "Home",   root_path %></li>
      <li><%= link_to "Help",    help_path %></li>
      <li><%= link_to "Sign in", signup_path %></li>
    </ul>
  </nav>
 </div>
 </div>

4

1 回答 1

0

+1 用于提供指向您的 Github 帐户的链接,因为不幸的是,您提供的代码并未指出问题出在哪里。

问题是在您的app/views/layouts/application.html.erb文件中,您有以下行:

<title><%= full_title(yield(:title)) %></title>

但是在app/views/static_pages下的视图中,您还没有provided:title被编辑的 s yield。例如,在您的app/views/static_pages/about.html.erb文件中,您需要添加以下行:

<% provide(:title, 'About Us') %>

您还需要将相关行添加到app/views/static_pages下的其他视图中,以使测试通过。有关详细信息,请参阅Rails Tutorial github repo中的static_pages目录下的视图

于 2013-03-31T06:52:44.373 回答