1
votes

I am working through the Ruby on Rails Tutorial by Michael Hartl and I keep getting a failing test that I cannot figure out. Here is a link to the code if you'd like code at github

Here is the failure I am getting:

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

spec/requests/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/' %>

app/views/layouts/_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>

app/views/layouts/_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>

1

1 Answers

0
votes

+1 for providing a link to your Github account as the code you've provided unfortunately doesn't indicate where the problem is.

The issue is that in your app/views/layouts/application.html.erb file, you have the line:

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

but in your views under app/views/static_pages, you have not provided the :titles that are yielded to. For example, in your app/views/static_pages/about.html.erb file, you need to add the following line:

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

You'll also need to add the relevant line to your other views under app/views/static_pages in order to get the test to pass. See the the views under the static_pages directory in Rails Tutorial github repo for details.