我正在使用Rails 3.0.10构建我的第一个Web应用程序,并试图在我的Pages_Controller_Spec中正确编写标题测试,就像我从Ruby on Rails教程书籍中学到的一样。然而,即使浏览器中的标题是正确的,测试也失败了。我已经安装了Capybara,但还没有使用它-这可能会有影响吗?
您可以看到,现在它非常基础,但我想从头开始编写一个坚实的测试套件。非常感谢任何帮助!
这是我的spec:(简单的“should be_success”通过得很好)
我将应用程序布局中的标题呈现如下:
编辑2:添加什么可以放置response.body。
您可以看到,现在它非常基础,但我想从头开始编写一个坚实的测试套件。非常感谢任何帮助!
这是我的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>
has_selector?
方法只适用于可见内容吗?你尝试过使用page.html.should match '<title>Home</title>'
或类似的方法吗? - Frost