我正在 Rails 3.0.10 上构建我的第一个 webapp 并尝试在我的 Pages_Controller_Spec 中正确我的标题测试,正如我从 ruby on rails 教程书中学到的那样,但是即使标题在浏览器中是正确的,测试也是失败。我已经安装了 Capybara,但还没有使用它 - 这可能会造成干扰吗?
您现在会发现它非常基础,但我想从头开始使用可靠的测试套件。任何帮助将不胜感激!
这是我的规范:(简单的“should be_success pass fine”)
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 => "Home")
end
end
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
it "should have the right title" do
get 'contact'
response.should have_selector("title", :content => "Contact")
end
end
end
我在应用程序布局中呈现标题是这样的:
<!DOCTYPE html>
<html>
<head>
<title><%= @title %></title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
下面是我如何设置实例变量:
<% @title = "Home" %>
<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>
编辑:这是测试输出
Failures:
1) PagesController GET 'home' should have the right title
Failure/Error: response.should have_selector("title",
expected css "title" to return something
# ./spec/controllers/pages_controller_spec.rb:15:in `block (3 levels) in <top (required)>'
2) PagesController GET 'contact' should have the right title
Failure/Error: response.should have_selector("title", :content => "Contact")
expected css "title" to return something
# ./spec/controllers/pages_controller_spec.rb:29:in `block (3 levels) in <top (required)>'
Finished in 0.14245 seconds
4 examples, 2 failures
编辑 2:添加放置 response.body 的内容
Running: spec/controllers/pages_controller_spec.rb
.<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<script src="/javascripts/prototype.js?1315409404" type="text/javascript"></script>
<script src="/javascripts/effects.js?1315409404" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1315409404" type="text/javascript"></script>
<script src="/javascripts/controls.js?1315409404" type="text/javascript"></script>
<script src="/javascripts/rails.js?1315409404" type="text/javascript"></script>
<script src="/javascripts/application.js?1315409404" type="text/javascript"></script>
</head>
<body>
<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>
</body>
</html>
最佳答案
原来我没有将 WebRat 作为 gem 安装。我猜这是因为我从 railscast 获取了我的测试精华,并使用了我在 Hartl 教程中学到的一些测试。
感谢您的帮助!
关于ruby-on-rails - 测试标题不适用于 rspec/capybara,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7354865/