0

我正在阅读 Michaels Hartls Ruby on Rails 教程。在第 3 章中,我按照说明设置了 R Spec 和自动测试。到目前为止,我已经完成了教程中指示的所有内容,但我不断收到此错误消息。

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/.. 运行我的测试时,我仍然会遇到这些错误。

有人可以帮忙吗?

4

1 回答 1

1

你需要设置@base_title. 您可以在使用它之前在规范中的任何位置设置它,但由于它在任何地方都是相同的,您可以将它放在一个before块中,即

before(:each) do
  @base_title = ...
end

你也可以使用let(但是规范需要使用base_title而不是@base_title

于 2012-04-27T20:04:07.867 回答