1

何が失敗の原因なのかわかりません!これはstackoverflowに関する私の最初の投稿なので、必要な情報をいくつか省略してしまった場合は、事前にお詫び申し上げます。

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

1 に答える 1

0

アプリケーションレイアウトがタイトルを生成しているため、テストは失敗しますが、タイトルをどこにも設定していません。タイトルなどのビューで表示する場合yield :something、ビューは「:something」について認識しているすべてのコンテンツをレンダリングすることを意味します。あなたの場合、タイトルは次のように生成されます。

<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を確認してください。

于 2012-06-10T12:56:34.670 に答える