测试标题不适用于 rspec/capybara

发布于 2024-12-03 19:26:11 字数 2951 浏览 0 评论 0原文

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

您现在会发现它非常基础,但我想从头开始使用可靠的测试套件。任何帮助将不胜感激!

这是我的规范:(简单的“应该成功通过”)

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:添加内容放置响应体

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2024-12-10 19:26:11

结果我没有将 WebRat 作为 gem 安装。我想这是因为我从 Railscast 中获取了测试精华并使用了我在 Hartl 教程中学到的一些测试。

感谢您的帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文