I am working through Michaels Hartls Ruby on Rails Tutorial. in Chapter 3 I set up R Spec and autotest as instructed. So far I have done everything that was instructed in the tutorial but I keep getting this error message.
rspec ./spec/controllers/pages_controller_spec.rb:13 # PagesController GET 'home' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:31 # PagesController GET 'contact' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:43 # PagesController GET 'about' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:57 # PagesController GET 'help' should have the right title'
My Gemfile Shows
source 'https://rubygems.org'
gem 'rails', '3.2.3'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
gem 'heroku'
group :development do
gem 'autotest', '4.4.6'
gem 'rspec-rails', '2.9.0'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
group :production do
# gems specifically for Heroku go here
gem 'pg'
end
group :test do
gem 'rspec', '2.9.0'
gem 'webrat', '0.7.3'
gem "spork", '0.9.0'
end
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platform => :ruby
gem 'execjs'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# 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 'ruby-debug19', :require => 'ruby-debug'
My application.html.erb shows
<!DOCTYPE>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
My page_controller_spec.rb shows this
require 'spec_helper'
describe PagesController do
render_views
#home
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => @base_title + " | Home")
end
it "should have a non-blank body" do
get 'home'
response.body.should_not =~ /<body>\s*<\/body>/
end
end
#contact
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
it "should have the right title" do
get 'contact'
response.should have_selector("title",
:content => @base_title + " | Contact")
end
end
#about
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
it "should have the right title" do
get 'about'
response.should have_selector("title",
:content => @base_title + " | About")
end
end
#help
describe "GET 'help'" do
it "should be successful" do
get 'help'
response.should be_success
end
it "should have the right title" do
get 'help'
response.should have_selector("title",
:content => @base_title + "| Help")
end
end
end
pages_helper.rb has
module PagesHelper
# Return a title an a per-page basis
def title
base_title = "Ruby on Rails Tutorial Sample App"
if @title.nil?
base_title
else
"#{base_title} | #{@title}"
end
end
end
pages_controller.rb has
class PagesController < ApplicationController
def home
@title = 'home'
end
def contact
@title = 'contact'
end
def about
@title = 'about'
end
def help
@title = 'help'
end
end
Everything shows good in the browsers. But I still get these errors when my test are run using rspec spec/..
Can someone help?
You need to set
@base_title. You could set it anywhere in the spec prior to your use of it, but since it’s the same everywhere you could put it in abeforeblock, ieYou could also use
let(but then the specs need to usebase_titlerather than@base_title