开发者

Testing for Title Not Working with rspec/capybara

开发者 https://www.devze.com 2023-04-03 10:35 出处:网络
I\'m building my first webapp on Rails 3.0.10 and trying to right my title tests in my Pages_Controller_Spec as I learned from the ruby on rails tutorial book, however even though the titles are corre

I'm building my first webapp on Rails 3.0.10 and trying to right my title tests in my Pages_Controller_Spec as I learned from the ruby on rails tutorial book, however even though the titles are correct in the browser, the tests are failing. I have installed Capybara, but haven't used it yet - is that 开发者_运维技巧potentially interfering?

You'll see it's very basic right now, but I want to start from the ground up with a solid testing suite. Any help would be much appreciated!

Here is my spec: (The simple "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

I'm rendering the title in the application layout like this:

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

<%= yield %>

</body>
</html>

Here is how I'm setting the instance variable:

<% @title = "Home" %>

<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>

EDIT: Here is the test output

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

EDIT 2: Adding what puts 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>


Turns out I didn't have WebRat installed as a gem. I guess this came from me taking my testing gems from railscast and using some of the tests I learned in the Hartl tutorial.

Thanks for the help!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号