RoR 教程(第 2 版)3.3.4 无法通过 Rspec 测试
RoR Tutorial (2nd Ed.) 3.3.4 Can't get Rspec test to pass
Rails 新手,正在阅读本书教程。近一天来我一直在为这个问题挠头,所以我需要一些帮助。从第 2 版书的第 3.3.4 节无法通过此测试。 (Ruby 在 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>
宝石文件
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
如果有人能为我指出正确的方向,将不胜感激。我更喜欢坚持最接近书本方法的解决方案,但如果我必须做一些不同的事情来让它通过并继续前进,那么我会尽我所能。只是请不要建议我做任何疯狂的高级事情,如果可能的话,我显然应该进入。谢谢。这里越简单越好。
您在规范中使用了 RSpec
和 Capybara
匹配器,但您没有安装这些 gem。
这是第 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 开发。您可以在这里免费阅读:
Rails 新手,正在阅读本书教程。近一天来我一直在为这个问题挠头,所以我需要一些帮助。从第 2 版书的第 3.3.4 节无法通过此测试。 (Ruby 在 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>
宝石文件
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
如果有人能为我指出正确的方向,将不胜感激。我更喜欢坚持最接近书本方法的解决方案,但如果我必须做一些不同的事情来让它通过并继续前进,那么我会尽我所能。只是请不要建议我做任何疯狂的高级事情,如果可能的话,我显然应该进入。谢谢。这里越简单越好。
您在规范中使用了 RSpec
和 Capybara
匹配器,但您没有安装这些 gem。
这是第 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 开发。您可以在这里免费阅读: