【发布时间】:2013-01-12 06:21:09
【问题描述】:
当它们都应该成功时,我有 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>
“内容”和“关于”的错误完全相同
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
home.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
【问题讨论】:
-
您的 Gemfile 说您使用的是什么版本的 Capybara。如果您使用的是
>= 2.0版本,您将无法看到title属性,因此为了教程的缘故,将版本设置为1.1.2。
标签: ruby-on-rails capybara rspec-rails railstutorial.org