2013-03-31 70 views
1

我正在通過邁克爾哈特爾Ruby on Rails Tutorial工作,我一直在得到一個我無法弄清楚的失敗測試 這是一個指向代碼的鏈接,如果你倒是像code at github預期的CSS「標題」與文本「Ruby on Rails教程示例應用程序|

這是我得到的失敗:

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

+1提供鏈接到您的Github上的帳戶爲你遺憾的是所提供的代碼不表示問題出在哪裏。

的問題是,在你的應用程序/視圖/佈局/ application.html.erb文件,你也行:

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

但在your views under app/views/static_pages,你沒有provide d的:title s表示是yield編輯。例如,在你的應用程序/視圖/ 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

+1

固定。我非常確定在教程中早些時候被刪除了,我甚至嘗試過,但是那裏(多次)並且仍然出現錯誤。但現在它正在工作,感謝修復。 – mcadamsjustin