2012-06-10 76 views
1

我無法弄清楚是什麼導致了失敗!這是我的計算器上的第一篇文章,所以我提前道歉,如果我離開了一些必要的信息..還好這裏是失敗...rspec ./spec/requests/static_pages_spec失敗。 Michael Hartl的指南中的第5章

Failures: 

1) Static pages Help page 
Failure/Error: it { should have_selector('title', text: full_title('Help')) } 
    expected css "title" with text "Ruby on Rails Tutorial Sample App | Help" to return something 
# ./spec/requests/static_pages_spec.rb:19:in `block (3 levels) in <top (required)>' 

2) Static pages About page 
Failure/Error: it { should have_selector('title', text: full_title('About Us')) } 
    expected css "title" with text "Ruby on Rails Tutorial Sample App | About Us" to return something 
# ./spec/requests/static_pages_spec.rb:26:in `block (3 levels) in <top (required)>' 

3) Static pages Contact page 
Failure/Error: it { should have_selector('title', text: full_title('Contact')) } 
    expected css "title" with text "Ruby on Rails Tutorial Sample App | Contact" to return something 
# ./spec/requests/static_pages_spec.rb:33:in `block (3 levels) in <top (required)>' 

Finished in 0.35756 seconds 
9 examples, 3 failures 

Failed examples: 

rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Help page 
rspec ./spec/requests/static_pages_spec.rb:26 # Static pages About page 
rspec ./spec/requests/static_pages_spec.rb:33 # Static pages Contact page 

,這裏是我的routes.rb

SampleApp::Application.routes.draw do 
root to: 'static_pages#home' 

match '/help', to: 'static_pages#help' 
match '/about', to: 'static_pages#about' 
match '/contact', to: 'static_pages#contact' 

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_selector('h1', text: 'Sample App') } 
it { should have_selector('title', text: full_title('')) } 
it { should_not have_selector 'title', text: '| Home' } 
end 

describe "Help page" do 
before { visit help_path } 

it { should have_selector('h1', text: 'Help') } 
it { should have_selector('title', text: full_title('Help')) } 
end 

describe "About page" do 
before { visit about_path } 

it { should have_selector('h1', text: 'About') } 
it { should have_selector('title', text: full_title('About Us')) } 
end 

describe "Contact page" do 
before { visit contact_path } 

it { should have_selector('h1', text: 'Contact') } 
it { should have_selector('title', text: full_title('Contact')) } 
end 
end 

contact.html.erb

<h1>Contact</h1> 
<p> 
Contact Ruby on Rails Tutorial about the sample app at the 
<a href="http://railstutorial.org/contact">contact page</a>. 
</p> 

application.html.erb

<!DOCTYPE html> 
<html> 
<head> 
<title><%= full_title(yield(:title)) %></title> 
<%= stylesheet_link_tag "application", media: "all" %> 
<%= javascript_include_tag "application" %> 
<%= csrf_meta_tags %> 
<%= render 'layouts/shim' %>  
</head> 
<body> 
<%= render 'layouts/header' %> 
<div class="container"> 
    <%= yield %> 
    <%= render 'layouts/footer' %> 
</div> 
</body> 
</html> 
+1

您沒有任何'content_for更多信息:title'在contact.html.erb,所以你的標題標籤不應該包含蝨你期望 – apneadiving

+1

我是不跟隨你抱歉。你可以請嘗試再解釋一次嗎? – lfkwtz

+0

你的'yield(:title)'不會做任何事情,除非你在'contact.html.erb'中添加'content_for:title' – apneadiving

回答

0

你的測試失敗了,因爲你的應用程序佈局得到題,但你是不是在任何地方設置標題。當您在視圖中看到yield :something時,例如對於標題,這意味着該視圖將呈現所知道的有關「:something」的任何內容。在你的情況,標題正在通過yieled如下:

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

爲了告訴Rails你想要什麼樣的內容「稱號」屈服,你需要設置它在你的觀點:

content_for(:title) { 'My Title'} 

它可以是乏味的在每個視圖重複這一點,所以很多人會爲標題幫手:

module ApplicationHelper 
    def title(page_title = '') 
    content_for(:title) { page_title } 
    end 
end 

然後在你看來,你可以這樣做:

<%= title 'My Title' %> 

退房Railscast #30對這種技術

+0

謝謝!採取了一些修補,但我現在得到它!乾杯。 – lfkwtz

相關問題