Request spec failing from Rails Tutorial from Michael Hartl - ruby-on-rails

I have a request spec that is failing even thought it should be passing. ALL of the content the spec is looking for appears in the source ( HTML ) of the app. These test should be passing as far as I can tell.I'm going to list in order:
The test
The helper helper method full_title that is used in the test
The relevant view templates in haml
The HTML output of the home page or root route
The Rspec failures
The Gemfile
The layout file
Here is the spec code:
require 'spec_helper'
describe "Static pages" do
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
Here is the helper method full_title:
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
The home view or /home route:
.center.hero-unit
%h1
Welcome to the Sample App
%h2
This is the home page for the
%a{ href: "http://railstutorial.org"}Ruby on Rails Tutorial sample app
= link_to "Sign up now!", '#', class: "btn btn-large btn-primary"
= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org'
The about page:
- provide(:title, 'About Us')
%body
%h1
About Us
%p
The
%a(href="http://railstutorial.org/")Ruby on Rails Tutorial
is a project to make a book and screencasts to teach web development with
%a(href="http://rubyonrails.org/")Ruby on Rails
This is the sample application for the tutorial
The help page:
- provide(:title, 'Help')
%body
%h1
Help
%p
Get help on the Ruby on Rails Tutorial at the
%a(href="http://railstutorial.org/help")Rails tutorial help page
To get help on this sample app, see the
%a(href="http://railstutorial.org/book")Rails Tutorial book
The contact page:
- provide(:title, 'Contact')
%h1 Contact
%p
Contact ruby on Rails Tutorial about the sample app at the
%a{ href: "http://railstutorial.org/contact"}Contact page
The HTML for the home page:
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App</title>
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/custom.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/static_pages.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/static_pages.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="KLYiW09+IfyIcxG2jcCX8tt3vts7aCTzYuiYA0ks8tM=" name="csrf-token" />
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
[![endif]-->
</head>
<body>
<header class='navbar navbar-fixed-top'>
<div class='navbar-inner'>
<div class='container'>
sample app
<nav>
<ul class='nav pull-right'>
<li>Home</li>
<li>help</li>
<li>Sign in</li>
</ul>
</nav>
</div>
</div>
</header>
<div class='container'>
<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 sample app</a>
</h2>
Sign up now!
</div>
<img alt="Rails" src="/assets/rails.png" />
<footer class='footer'>
<small>
<a href='http://railstutorial.org'>Rails Tutorial by Michael Hartl</a>
</small>
<nav>
<ul>
<li>About</li>
<li>Contact</li>
<li>
<a href='http://railstutorial.org'>News</a>
</li>
</ul>
</nav>
</footer>
</div>
</body>
</html>
The Rspec failures:
FF.FFFFFF
Failures:
1) Static pages Home page
Failure/Error: it { should have_selector('h1', text: 'Sample App') }
expected css "h1" with text "Sample App" to return something
# ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
2) Static pages Home page
Failure/Error: it { should have_selector('title', text: full_title('')) }
expected css "title" with text "Ruby on Rails Tutorial Sample App" to return something
# ./spec/requests/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
3) Static pages Help page
Failure/Error: it { should have_selector('h1', text: 'Help') }
expected css "h1" with text "Help" to return something
# ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
4) 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:17:in `block (3 levels) in <top (required)>'
5) Static pages About page
Failure/Error: it { should have_selector('h1', text: 'About') }
expected css "h1" with text "About" to return something
# ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'
6) 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:24:in `block (3 levels) in <top (required)>'
7) Static pages Contact page
Failure/Error: it { should have_selector('h1', text: 'Contact') }
expected css "h1" with text "Contact" to return something
# ./spec/requests/static_pages_spec.rb:30:in `block (3 levels) in <top (required)>'
8) 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:31:in `block (3 levels) in <top (required)>'
Finished in 0.34035 seconds
9 examples, 8 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:8 # Static pages Home page
rspec ./spec/requests/static_pages_spec.rb:9 # Static pages Home page
rspec ./spec/requests/static_pages_spec.rb:16 # Static pages Help page
rspec ./spec/requests/static_pages_spec.rb:17 # Static pages Help page
rspec ./spec/requests/static_pages_spec.rb:23 # Static pages About page
rspec ./spec/requests/static_pages_spec.rb:24 # Static pages About page
rspec ./spec/requests/static_pages_spec.rb:30 # Static pages Contact page
rspec ./spec/requests/static_pages_spec.rb:31 # Static pages Contact page
The Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'haml-rails'
gem 'bootstrap-sass', '2.0.0'
gem 'capybara', '1.1.2'
group :development, :test do
gem 'sqlite3' , '1.3.5'
gem 'rspec-rails', '2.10.0'
end
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'jquery-rails'
group :test do
gem 'capybara', '1.1.2'
end
group :production do
gem 'pg', '0.12.2'
end
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'
The layout file
!!! 5
%html
%head
%title= full_title(yield(:title))
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
= render 'layouts/shim'
%body
= render 'layouts/header'
.container
= yield
= render 'layouts/footer'

This is a very subtle error for a new programmer to debug. I'm using the vim editor and for some reason 4 spaces was expanding to a tab. A hidden tab character in the layouts file was causing the specs to fail.

Related

RoR , trouble with full_title

I am on Chapter 4 of Michael Hartl's RoR tutorial, and having some troubles with rspec and using the full_title helper function. In part 4.1 of the tutorial, I have the helper code as follows.
app/helpers/application_helper.rb
module ApplicationHelper
# Returns the full title on a per-page basis.
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/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
let(:base_title) {"Ruby on Rails Tutorial Sample App"}
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the base title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
expect(page).not_to have_title('| Home')
end
end
end
app/views/static_pages/home.html.erb
<h1>Sample App</h1>
<p>this is the home page for the Ruby on Rails Tutorial
sample application.</p>
And when I try to run test I get this:
gvyntyk#gvyntyk-r60:~/rails_projects/sample_app$ rspec spec/requests/static_pages_spec.rb
FFF
Failures:
1) Static pages Home page should not have a custom page title
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0xb0ea1cc>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:20:in `block (3 levels) in <top (required)>'
2) Static pages Home page should have the base title
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0x961e58c>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:15:in `block (3 levels) in <top (required)>'
3) Static pages Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0x9c13244>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:10:in `block (3 levels) in <top (required)>'
Finished in 1.23 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Home page should not have a custom page title
rspec ./spec/requests/static_pages_spec.rb:14 # Static pages Home page should have the base title
rspec ./spec/requests/static_pages_spec.rb:9 # Static pages Home page should have the content 'Sample App'
Can anyone tell me what's going wrong here?
It feels like you've not posted the exact code for what the tests are running against. I'm not seeing how by what you've posted the code is even getting to full_title in the helper as you only have yeild(:title) in application.html.erb. Nothing is calling full_title.
Looking at Michaels example in the book you need to have full_title(yield(:title)) in the title tags removing what you've got posted there.

Rails tutorial chapter 3 Rspec test failing

So I have been trying to follow along with the rails tutorial but I have been stuck on making the title slightly dynamic. Once I get to this part and run
$ bundle exec rspec spec/requests/static_pages_spec.rb
I receive the following error
Failures:
1) Static pages Help page should have the title 'Help'
Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
expected #has_title?("Ruby on Rails Tutorial Sample App | Help") to return true, got false
# ./spec/requests/static_pages_spec.rb:27:in `block (3 levels) in <top (required)>'
2) Static pages Home page should have the title 'Home'
Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
expected #has_title?("Ruby on Rails Tutorial Sample App | Home") to return true, got false
# ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'
3) Static pages About page should have the title 'About Us'
Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
expected #has_title?("Ruby on Rails Tutorial Sample App | About Us") to return true, got false
# ./spec/requests/static_pages_spec.rb:40:in `block (3 levels) in <top (required)>'
Finished in 0.1826 seconds
6 examples, 3 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:25 # Static pages Help page should have the title 'Help'
rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page should have the title 'Home'
rspec ./spec/requests/static_pages_spec.rb:38 # Static pages About page should have the title 'About Us'
This is what my Home, Help, and About views are like.
Home
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
Help
<% provide(:title, 'Help') %>
<h1>Help</h1>
<p>
Get help on the Ruby on Rails Tutorial at the
Rails Tutorial help page.
To get help on this sample app, see the
Rails Tutorial book.
</p>
About
<%= provide(:title, 'About Us') %>
<h1>About Us</h1>
<p>
The Ruby on Rails Tutorial
is a project to make a book and screencasts to teach web development
with Ruby on Rails. This
is the sample application for the tutorial.
</p>
And this is my Application view
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield (:title) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
And my Static Page spec
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help page" do
it "should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
end
end
and lastly my gemfile
source 'https://rubygems.org'
ruby '2.1.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.2'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Change your view like below(notice = before provide in the first line). Do the same for all your three views
Home
<%= provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
Have you tried going to each page manually and checking the title yourself?
I pasted literally everything you provided into a project (I did the tutorial not long ago so I already had one set up with the static_pages controller) and all those tests passed. I would have said that you might have mixed up the filenames for your views, or the names in the routes file, but the content tests are passing so that can't be it. I guess it's possible that one of your files has been saved in the wrong encoding by your editor, or something else along those lines. Also check that you have in fact saved all your files before running your tests.
Also a quick note that = is NOT necessary for <% provide ... so you should probably remove it from your about view.
I think you may have forgotten to add the 'contact' page to your routes.rb file, like so:
SampleApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
get "static_pages/contact"
Try this :
static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
#expect(page).to have_title("#{base_title} | Home")
page.should have_title("#{base_title} | Home")
end
end
describe "Help page" do
it "should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
#expect(page).to have_title("#{base_title} | Help")
page.should have_title("#{base_title} | Help")
end
end
describe "About page" do
it "should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
page.should have_title("#{base_title} | About Us")
end
end
describe "Contact page" do
it "should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
it "should have the title 'Contact'" do
visit '/static_pages/contact'
# expect(page).to have_title("#{base_title} | Contact")
page.should have_title("#{base_title} | Contact")
end
end
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title)%></title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
gem 'capybara', '2.1.0'
Becarful with spaces in the title

RSpec spec fail and I can't figure it out

I hate to be a help-vampire, but I'm new and I've been looking at this almost two days.
I'm working on Ruby on Rails Tutorial by Michael Hartl. I'm running the test in listing 4.4, which I will include below. The specific test that I'm trying to get to pass checks to see if the page has a selector with the text, "Ruby on Rails Tutorial Sample App". I'll probably find the answer to this problem during the process of asking it on SO, but here we go. Below are, what I believe, the relevant files:
/spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "StaticPages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the base title" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
page.should_not have_selector('title', :text => '| Home')
end
end
...[other tests for other pages, let me know if you think they're necessary]
end
app/helpers/application_helper.rb
module ApplicationHelper
# It is a good idea to put controller specific helpers in their
# related helper file (e.g., helpers specifically for the
# StaticPages controller).
# Returns the full title on a per-page basis:
# This helper will be used for each page's title (unless
# otherwise noted).
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/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title))%></title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
app/views/static_pages/home.html.erb
<% provide :title, "Home" %>
<h1>Sample App</h1>
<p>
This is the home page for a
Ruby on Rails tutorial.
</p>
<p>
It's by Michael Hartl
</p>
I have not configured Sublime to run rspec in the console, yet. When I run the test in Terminal from a bash, I get the following:
.......F.
Failures:
1) StaticPages Home page should have the base title
Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
expected #has_selector?("title", {:text=>"Ruby on Rails Tutorial Sample App"}) to return true, got false
# ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'
Finished in 0.14273 seconds
9 examples, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:12 # StaticPages Home page should have the base title
Randomized with seed #####
When I look at the source code in chrome, I can clearly see a pair of tags with "Ruby on Rails Tutorial Sample App | Home" in between them.
Well, I've gone through it all and I can't figure out what's going on. Hello, SO world! Please help. Thank you.
I would push to github right now, but they're down. I'll update when possible.
change
have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
to
have_title("Ruby on Rails Tutorial Sample App | Home")
It's a change to Capybara and was updated in Michael Hartl's tutorial. To see the update search for "Change have_selector(’title’, …) to have_title(…)" once in the tutorial.

Ruby on Rails embedded ruby generating errors

I am working my way through Ruby on Rails 2nd Ed., Hartl. In section 3.3.3 my tests failed after I removed some static text with some embedded Ruby. The test was passing previously no problem.
rspec
require 'spec_helper'
describe "Home pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home")
end
end
end
home.html.erb
<% provide (:title, 'Home') %>
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
</body>
</html>
error from console
C:\Sites\rails_projects\sample_app>bundle exec rspec spec/requests/static_pages_spec.rb
?[31mF?[0m?[31mF?[0m?[32m.?[0m?[32m.?[0m?[32m.?[0m?[32m.?[0m
Failures:
1) Home pages Home page should have the h1 'Sample App'
?[31mFailure/Error:?[0m ?[31mvisit '/static_pages/home'?[0m
?[31mActionView::Template::Error:?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ',', expecting ')'?[0m
?[31m...putBuffer.new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ')', expecting keyword_end?[0m
?[31m....new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
?[36m # <internal:prelude>:10:in `synchronize'?[0m
?[36m # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'?[0m
2) Home pages Home page should have the title 'Home'
?[31mFailure/Error:?[0m ?[31mvisit '/static_pages/home'?[0m
?[31mActionView::Template::Error:?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ',', expecting ')'?[0m
?[31m...putBuffer.new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ')', expecting keyword_end?[0m
?[31m....new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
?[36m # <internal:prelude>:10:in `synchronize'?[0m
?[36m # ./spec/requests/static_pages_spec.rb:12:in `block (3 levels) in <top (required)>'?[0m
Finished in 0.51562 seconds
?[31m6 examples, 2 failures?[0m
Failed examples:
?[31mrspec ./spec/requests/static_pages_spec.rb:6?[0m ?[36m# Home pages Home page should have the h1 'Sample App'?[0m
?[31mrspec ./spec/requests/static_pages_spec.rb:11?[0m ?[36m# Home pages Home page should have the title 'Home'?[0m
Any help is appreciated
I think you need to remove the whitespace prior to the parenthesis
provide (:title, 'Home')
should be
provide(:title, 'Home')

RSpec Failure: "Static Pages X should have title X" -- Tried with have_xpath & have_selector

I've been scouring the web & Stack Overflow to tr and figure out how to get these tests working. I'm brand new to Ruby & to Rails, I'm simply following along Hartl's tutorial - copy pasting most of the code to see how it all falls together in the end.
Now, I'm getting stuck in section 3.3 "Slightly Dynamic Pages".
This is the error I'm recieving:
C:\Sites\sample_app>bundle exec rspec --no-color spec/requests/static_pages_spec.rb
F.F.F.
Failures:
1) Static pages About page should have the title 'About Us'
Failure/Error: page.should have_selector('title',
expected css "title" with text "Ruby on Rails Tutorial Sample App | About Us" to return something
# ./spec/requests/static_pages_spec.rb:44:in `block (3 levels) in <top (required)>'
2) Static pages Help page should have the title 'Help'
Failure/Error: page.should have_selector('title',
expected css "title" with text "Ruby on Rails Tutorial Sample App | Help" to return something
# ./spec/requests/static_pages_spec.rb:29:in `block (3 levels) in <top (required)>'
3) Static pages Home page should have the title 'Home'
Failure/Error: page.should have_selector('title',
expected css "title" with text "Ruby on Rails Tutorial Sample App | Home" to return something
# ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'
Finished in 3.09 seconds
6 examples, 3 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:42 # Static pages About page should have the title 'About Us'
rspec ./spec/requests/static_pages_spec.rb:27 # Static pages Help page should have the title 'Help'
rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page should have the title 'Home'
Randomized with seed 25648
The error is occuring as soon as I swtich from this HTML Structure (in my About/Home/Help.html.erb files):
<% provide(:title, 'Home') %>
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
</body>
</html>
To this:
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
Other related files:
Application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | Home")
// The line below is something I tried replacing the page.should have_selector with
// page.should have_xpath("//title", :text => "Home")
end
end
.... (other describe pages, same structure)
end
I might've just gone blind over the course of copying/pasting/reading from Hartl's tutorial, but I am pretty damn sure it looks the same way as he described it.
I've done my best at looking for a solution to this, but alas I haven't been able to figure it out, so here goes!
Cheers!
Edit: Answer to Fiona #1
On this url : "http://localhost:3000/static_pages/home"
The document title is nothing.
The source of the document looks like this:
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
There is no doctype, head, body or title declaration in the source.
For Rails to pick up the layout file, the application.html.erb file should be in app/views/layouts/application.html.erb.

Resources