1

我有一个失败的请求规范,即使它应该通过。规范正在寻找的所有内容都出现在应用程序的源代码 ( HTML ) 中。据我所知,这些测试应该通过了。我将按顺序列出:

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

这是规范代码:

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 

宝石文件

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

1 回答 1

2

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.

于 2012-10-30T03:11:12.433 回答