我正在使用Michaels Hartls Ruby on Rails教程。在第3章中,我按照说明设置了R Spec和autotest。到目前为止,我已经完成了教程中指示的所有内容,但我不断收到此错误消息。
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'
我的Gemfile显示
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'
我的application.html.erb显示
<!DOCTYPE>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
我的page_controller_spec.rb显示了这个
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有
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有
class PagesController < ApplicationController
def home
@title = 'home'
end
def contact
@title = 'contact'
end
def about
@title = 'about'
end
def help
@title = 'help'
end
end
浏览器中的一切都很好。但是当我的测试使用rspec spec / ..
运行时,我仍然会遇到这些错误有人可以帮忙吗?
答案 0 :(得分:1)
您需要设置@base_title
。您可以在使用它之前在规范中的任何位置设置它,但因为它在任何地方都可以将它放在before
块中,即
before(:each) do
@base_title = ...
end
您也可以使用let
(但规格需要使用base_title
而不是@base_title