当它们都应该成功时,我有 3 个 rspec 选择器失败。我跟随 rails-tutorial.org 的书和他的节目是正确的。
PagesController GET 'home' should have the right title
Failure/Error: response.should have_selector("title", :content => "Ruby on Rails Sample App | Home")
expected following output to contain a <title>Ruby on Rails Sample App | Home</title> tag:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Ruby on Rails Tutorial Sample App | Home</title>
</head>
以及“内容”和“关于”的完全相同的错误
应用程序.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
主页.html.erb
<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>
application_helper.erb
module ApplicationHelper
#Return a title on a per-page basis
def title
base_title = "Ruby on Rails Tutorial Sample App"
if @title.nil?
base_title
else
"#{base_title} | #{@title}"
end
end
end
pages_controller_spec.rb
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title", :content => "Ruby on Rails Sample App | Home")
end
it "should have a non-blank body" do
get 'home'
response.body.should_not =~ /<body>\s*<\/body>/
end
end