RoR教程(第2版)3.3.4无法通过Rspec测试

时间:2015-02-20 18:06:02

标签: ruby-on-rails ruby rspec

Rails的新手并完成本书教程。我已经在这个问题上抓了近一天,所以我可以使用一些帮助。无法通过第2版本第3.3.4节的测试。 (Ruby on Rails教程)请参阅下面的失败和代码:

故障:

  1) Static pages Home page should have the title 'Home'
     Failure/Error: page.should have_selector('title', :text => 'Ruby on Rails Tutorial Sample App | Home')
     expected to find css "title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found 
     "", which matched the selector but not all filters.
     # ./spec/features/static_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  2) Static pages Help page should have the title 'Help'
     Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help")
     expected to find css "title" with text "Ruby on Rails Tutorial Sample App 
     | Help" but there were no matches. Also found 
     "", which matched the selector but not all filters.
     # ./spec/features/static_pages_spec.rb:28:in `block (3 levels) in <top (required)>'

  3) Static pages About page should have the title 'About Us'
     Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About")
     expected to find css "title" with text "Ruby on Rails Tutorial Sample App 
     | About" but there were no matches. Also found
     "", which matched the selector but not all filters.
     # ./spec/features/static_pages_spec.rb:41:in `block (3 levels) in <top (required)>'

static_pages_spec.rb

require 'spec_helper'
require 'rails_helper'

  describe "Static pages" do

    describe "Home page" do

      it "should have the h1 'Sample App'" do
        visit '/static_pages/home'
        page.should have_selector('h1', :text => 'Sample App')
      end

      it "should have the title 'Home'" do
        visit '/static_pages/home'
        page.should have_selector('title', 
                    :text => 'Ruby on Rails Tutorial Sample App | Home')
      end
    end

    describe "Help page" do

      it "should have the h1 'Help'" do
        visit '/static_pages/help'
        page.should have_selector('h1', :text => 'Help')
      end

      it "should have the title 'Help'" do
        visit '/static_pages/help'
        page.should have_selector('title', 
              :text => "Ruby on Rails Tutorial Sample App | Help")
      end
    end

    describe "About page" do

      it "should have the h1 'About Us'" do
        visit '/static_pages/about'
        page.should have_selector('h1', :text => 'About Us')
      end

      it "should have the title 'About Us'" do
        visit '/static_pages/about'
        page.should have_selector('title', 
              :text => "Ruby on Rails Tutorial Sample App | About")
      end
    end
  end

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  end
end

home.html.erb

<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
    This is the home page for the
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
</p>

help.html.erb

<% provide(:title, 'Help') %>
<h1>Help</h1>
<p>
    Get help on the Ruby on Rails Tutorial at the
    <a href="http://railstutorial.org/help">Rails Tutorial help page</a>
    . To get help on this sample app, see the
    <a href="http://railstutorial.org/book">Rails Tutorial book</a>.
</p>

about.html.erb

<% provide(:title, 'About Us') %>
<h1>About Us</h1>
<p>
    The <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    is a project to make a book and screencasts to teach web development
    with <a href="http://rubyonrails.org/">Ruby on Rails</a>. This
    is the sample application for the tutorial.
</p>

的Gemfile

source 'https://rubygems.org'


gem 'minitest'

gem 'test-unit'
gem 'rails_12factor'
ruby '2.1.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do


  gem 'sqlite3'

  gem 'capybara'

  gem 'rspec-rails'

  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :development do
    # Use sqlite3 as the database for Active Record
    gem 'sqlite3'
end

group :production do
    gem 'pg'
end

如果有人能让我指出正确的方向,我们将不胜感激。我更喜欢坚持最接近书籍方法的解决方案,但如果我必须采取不同的方式来实现这一目标并继续前进,那么我将采取我能得到的东西。只是请不要建议我做任何疯狂的进步,如果可能的话,我显然应该进入。谢谢。这里简单就好了。

1 个答案:

答案 0 :(得分:2)

您在规范中使用了RSpecCapybara匹配器,但是您没有安装这些宝石。

以下是第2版的Gemfile:

<强>的Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.16'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.11.0'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.5'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.2'

group :test do
  gem 'capybara', '1.1.2'
end

group :production do
  gem 'pg', '0.12.2'
end

查看您的 Gemfile,您似乎正在使用更新版本的Rails。如果您正在关注Michael的教程,则应使用他指定的相同的版本。有一个较新版本的教程,适用于Rails 4开发。你可以在这里免费阅读:

https://www.railstutorial.org/book