使用rspec/capybara测试标题无效

3
我正在使用Rails 3.0.10构建我的第一个Web应用程序,并试图在我的Pages_Controller_Spec中正确编写标题测试,就像我从Ruby on Rails教程书籍中学到的一样。然而,即使浏览器中的标题是正确的,测试也失败了。我已经安装了Capybara,但还没有使用它-这可能会有影响吗?
您可以看到,现在它非常基础,但我想从头开始编写一个坚实的测试套件。非常感谢任何帮助!
这是我的spec:(简单的“should be_success”通过得很好)
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>

测试的输出是什么? - Ransom Briggs
编辑了帖子以包括那些信息。 - Rapture
has_selector? 方法只适用于可见内容吗?你尝试过使用 page.html.should match '<title>Home</title>' 或类似的方法吗? - Frost
我原以为将'render_views'放在顶部可以使其在可见内容上工作。也许这在某些时候已经改变了? - Rapture
"puts response.body" 显示什么? - Andy Waite
我已经添加了输出response.body的内容 - 我猜它正在渲染视图!而且标题就在那里...看起来我在为这么小的事情烦恼...但我想要做到正确。 - Rapture
1个回答

6
原来我没有安装WebRat gem。我想这可能是因为我从railscast中获取了我的测试gems,并使用了Hartl教程中学到的一些测试。感谢您的帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接