从Michael Hartl的Rails教程请求规范失败

时间:2012-10-29 11:51:19

标签: ruby-on-rails rspec

我有一个请求规范甚至失败,甚至认为它应该通过。规范要查找的所有内容都显示在应用的源(HTML)中。据我所知,这些测试应该通过。我将按顺序列出:

  • 测试
  • 测试中使用的辅助助手方法full_title
  • haml中的相关视图模板
  • 主页或根路由的HTML输出
  • Rspec失败
  • Gemfile
  • 布局文件

以下是规范代码:

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

这是辅助方法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

主视图或/ home路径:

.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'

关于页面:

- 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

帮助页面:

- 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

联系页面:

- 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

主页的HTML:

<!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'>
          <a href="/" id="logo">sample app</a>
          <nav>
            <ul class='nav pull-right'>
              <li><a href="/">Home</a></li>
              <li><a href="/help">help</a></li>
              <li><a href="#">Sign in</a></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>
        <a href="#" class="btn btn-large btn-primary">Sign up now!</a>
      </div>
      <a href="http://rubyonrails.org"><img alt="Rails" src="/assets/rails.png" /></a>
      <footer class='footer'>
        <small>
          <a href='http://railstutorial.org'>Rails Tutorial by Michael Hartl</a>
        </small>
        <nav>
          <ul>
            <li><a href="/about">About</a></li>
            <li><a href="/contact">Contact</a></li>
            <li>
              <a href='http://railstutorial.org'>News</a>
            </li>
          </ul>
        </nav>
      </footer>
    </div>
  </body>
</html>

Rspec失败:

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 

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'

布局文件

!!! 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'

1 个答案:

答案 0 :(得分:2)

对于新程序员来说,这是一个非常微妙的错误。我正在使用vim编辑器,由于某种原因,4个空格正在扩展到选项卡。布局文件中隐藏的制表符会导致规范失败。