测试标题不适用于rspec/capybara

Rap*_*ure 3 testing rspec ruby-on-rails

我正在Rails 3.0.10上构建我的第一个webapp并尝试在我的Pages_Controller_Spec中修改我的标题测试,正如我从ruby on rails教程书中学到的那样,但是即使标题在浏览器中是正确的,测试也会失败.我已经安装了Capybara,但尚未使用它 - 是否有可能干扰?

你会发现它现在非常基础,但我想从一个坚实的测试套件开始.任何帮助将非常感激!

这是我的规范:(简单的"应该__ccess传递罚款)

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
Run Code Online (Sandbox Code Playgroud)

我正在应用程序布局中呈现标题,如下所示:

<!DOCTYPE html>
<html>
<head>
  <title><%= @title %></title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
</head>
<body>

<%= yield %>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是我如何设置实例变量:

<% @title = "Home" %>

<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>
Run Code Online (Sandbox Code Playgroud)

编辑:这是测试输出

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
Run Code Online (Sandbox Code Playgroud)

编辑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>
Run Code Online (Sandbox Code Playgroud)

Rap*_*ure 6

结果我没有安装WebRat作为gem.我想这来自于我从railscast中获取测试宝石并使用我在Hartl教程中学到的一些测试.

谢谢您的帮助!