2012-10-29 114 views
1

我有一個請求規範,即使認爲它應該傳遞失敗。該規範正在尋找的所有內容都出現在應用程序的源代碼(HTML)中。這些測試應儘可能使我可以去tell.I'm以列表:請求規範失敗從邁克爾哈特爾的Rails教程

  • 測試
  • 助手的輔助方法full_title是在測試
  • 相關視圖模板中使用在HAML
  • 主頁或根本途徑
  • RSpec的失敗
  • 的Gemfile中
  • 佈局文件的HTML輸出

這裏是規格代碼:

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 

首頁視圖或/家庭路線:

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

你的佈局視圖是什麼樣的? –

+0

可能是錯誤的,但是你不需要在你的規格頂部有一個'subject {page}'? –

+0

@shioyama是的,我願意。我剛剛添加它,但規格仍然不通過 – lampShade

回答

2

這是一個新的程序員調試一個非常微妙的錯誤。我正在使用vim編輯器,出於某種原因,4個空間正在擴展到一個選項卡。佈局文件中隱藏的製表符會導致規格失敗。

相關問題