我正在通过迈克尔·哈特尔的Ruby on Rails Tutorial工作,我一直在进行一项我无法弄清楚的失败测试。 如果您需要code at 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
应用/助手/ 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
应用\视图\ 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>
答案 0 :(得分:0)
+1用于提供指向您的Github帐户的链接,因为您提供的代码很遗憾并不能说明问题所在。
问题是,在 app / views / layouts / application.html.erb 文件中,您有以下行:
<title><%= full_title(yield(:title)) %></title>
但在your views under app/views/static_pages中,您provide
未:title
个yield
。例如,在 app / views / static_pages / about.html.erb 文件中,您需要添加以下行:
<% provide(:title, 'About Us') %>
您还需要将相关行添加到 app / views / static_pages 下的其他视图中,以便让测试通过。有关详细信息,请参阅the views under the static_pages directory in Rails Tutorial github repo。