我正在Rails 3.0.10上构建我的第一个webapp并尝试在我的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:添加什么把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>
答案 0 :(得分:6)
原来我没有安装WebRat作为gem。我想这来自于我从railscast中获取测试宝石并使用我在Hartl教程中学到的一些测试。
感谢您的帮助!